Skip to content

Add null check in StandardMultipartHttpServletRequest isEmpty() to prevent NPE #34782

@hyejungg

Description

@hyejungg

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions