When evaluating, match the fields using the get error method:
java.lang.NullPointerException: Cannot read the array length because "src" is null at java.base/java.util.Base64$Encoder.encode(Base64.java:291) ~[na:na] at java.base/java.util.Base64$Encoder.encodeToString(Base64.java:345) ~[na:na] at fqw.application.mapper.ResponseMapper.dataToBase64(ResponseMapper.java:31) ~[main/:na] .....
Mapper:
@Mapper
public interface ResponseMapper {
ResponseMapper INSTANCE = Mappers.getMapper(ResponseMapper.class);
@Mapping(target = "taskId", source = "id")
@Mapping(target = "imageName", source = "imageMetadata.imageName")
@Mapping(target = "imageData", source = "loadedImage.data",qualifiedByName = "dataToBase64")
@Mapping(target = "imageResultData", source = "resultImage.data",qualifiedByName = "dataToBase64")
@Mapping(target = "description", source = "imageMetadata.description")
@Mapping(target = "contentType", source = "imageMetadata.contentType")
ResponseDTO taskToResponseDTO(Task task);
@Named("dataToBase64")
default String dataToBase64(byte[] data){
return Base64.getEncoder().encodeToString(data);
}
}
what could be the problem?
To simplify the question, run this line of code first (you'll get the same error):
Base64.getEncoder().encodeToString(null);
// java.lang.NullPointerException: Cannot read the array length because "src" is null
An easy fix would be a simple null check - but you need to decide what do you want to do if the parameter is null. here are some options:
String dataToBase64(byte[] data){
if(Objects.isNull(data)) {
data = new byte[0];
}
return Base64.getEncoder().encodeToString(data);
}
String dataToBase64(byte[] data){
if(Objects.isNull(data)) {
return null;
}
return Base64.getEncoder().encodeToString(data);
}
Optional<String> dataToBase64(byte[] data){
return Optional.ofNullable(data)
.map(Base64.getEncoder()::encodeToString);
}