I got Possible null pointer dereference in convertMultiPartToFile(MultipartFile) due to return value of called method [line 91]
in my findbugs report.
Here is the code:
private File convertMultiPartToFile(MultipartFile file) throws IOException {
//line below is the line 91
if (file == null || file.getOriginalFilename() == null)
throw new InputValidationException("fileNameInvalid", i18n, file.getOriginalFilename());
File convFile = new File(file.getOriginalFilename());
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}
I already check the null value of the file, why do I still get the warning?
Update 1:
After I removed the file name in the exception, it still has a warning on the line below.
private File convertMultiPartToFile(MultipartFile file) throws IOException {
if (file == null || file.getOriginalFilename() == null)
throw new InputValidationException("fileNameInvalid", i18n, "");
File convFile = new File(file.getOriginalFilename()); // warning here
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}
Update 2:
private File convertMultiPartToFile(MultipartFile file) throws IOException {
File convFile = null;
if (file != null && file.getOriginalFilename() != null) {
convFile = new File(file.getOriginalFilename()); //line 91
try (FileOutputStream fos = new FileOutputStream(convFile);) {
fos.write(file.getBytes());
}
}
return convFile;
}
Adapted @Michael Peacock's answer, the warning is still there.
Possible null pointer dereference in convertMultiPartToFile(MultipartFile) due to return value of called method
Bug type NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE (click for details) In class com.corpobids.server.service.AwsAPIService In method convertMultiPartToFile(MultipartFile)
Local variable stored in JVM register ?
Method invoked at Service.java:[line 91]
Known null at Service.java:[line 91]