-
Couldn't load subscription status.
- Fork 38.8k
Description
Summary
Invoking MultipartFile.isEmpty() when MultipartFile is null results in 'NullPointerException'.
When an image is imported to the client, it is usually null if the image is not uploaded. In this case, developers often assume that both null and empty cases are handled by the presumption that '!image.isEmpty()' is used for brevity.
Currently, StandardMultipartHttpServletRequest#isEmpty() calls 'this.part.getSize()' directly without confirming that it is 'null', which can result in NPE.
Here’s the exception I encountered:
java.lang.NullPointerException: Cannot invoke "org.springframework.web.multipart.MultipartFile.isEmpty()" because "image" is null
Proposal
It would be helpful if isEmpty() were made more defensive by returning true when this.part == null, similar to how StringUtils.isEmpty() treats null as empty.
Suggested implementation:
@Override
public boolean isEmpty() {
return (this.part == null || this.part.getSize() == 0);
}For those familiar with utility-style isEmpty() methods such as StringUtils, more secure null handling will improve the developer experience.
If this change makes sense, I'm willing to open a pull request!