Giorgio Di Feola :
I'm processing two different multipart files in my Spring controller.
Both files are then sent on to a service to set the entities. But a NullPointerException
is being thrown if both or one is null. How do check if either of the two files (projectImg
/chartImg
) is null?
Here's my code so far:
public void uploadImages(MultipartFile projectImg, MultipartFile chartImg, Long projectId) throws ValidationException, IOException {
Project project = projectRepository.findOne(projectId);
Project save = projectRepository.save(project);
int maximumSizeMB = 15000000;
if (!projectImg.isEmpty()) {
if (projectImg.getSize() > maximumSizeMB) {
throw new ValidationException("Image size is too big. Maximum size is 15 MB");
}
byte[] projectFile = ImageCompression.compressImage(projectImg);
project.setProjectImg(projectFile);
save.getProjectImg();
}
if (!chartImg.isEmpty()) {
if (chartImg.getSize() > maximumSizeMB) {
throw new ValidationException("Image size is too big. Maximum size is 15 MB");
}
byte[] chartFile = ImageCompression.compressImage(chartImg);
project.setChartImg(chartFile);
save.getChartImg();
}
projectRepository.save(project);
}
Thanks!
Frakcool :
Just for the sake of you to accept an answer and this question doesn't stay as "unanswered", I'll post my comment as an answer:
You can call
if (projectImg != null) { ... }
before or instead
if (projectImg.isEmpty()) { ... }
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=467066&siteId=1