Suggestion: Add null message for HttpRequest.Builder POST()
Jaikiran Pai
jaikiran.pai at oracle.com
Sun Nov 2 05:37:32 UTC 2025
This proposal looks reasonable to me. I've filed
https://bugs.openjdk.org/browse/JDK-8371091 as an enhancement request
and you can raise a PR against that.
-Jaikiran
On 02/11/25 12:44 am, Hyunsu Eun wrote:
> Hello,
>
> While reviewing the implementation of java.net.http.HttpRequest.Builder,
> I noticed a small inconsistency in how null arguments are handled.
>
> Currently, when calling POST(null), a plain NullPointerException is thrown
> without any descriptive message.
>
> Example:
>
> HttpRequest.newBuilder()
> .uri(URI.create(“https://example.com”))
> .POST(null)
> .build();
>
> This produces the following stack trace:
>
> Exception in thread “main” java.lang.NullPointerException
> at java.base/java.util.Objects.requireNonNull(Objects.java:220)
> at java.net.http/jdk.internal.net.http.HttpRequestBuilderImpl.POST(HttpRequestBuilderImpl.java:180)
> at org.example.Main.main(Main.java:17)
>
> Although the cause can be inferred from Main.java:17,
> the exception message itself does not clearly indicate which argument was null.
>
> In contrast, the uri(URI) method in the same class uses
> requireNonNull(uri, "uri must be non-null"),
> which provides a clear and descriptive message when the argument is null.
>
> I understand that URI is a mandatory field for every request,
> while the BodyPublisher might be optional.
> However, since passing a null body to POST, PUT, or
> method(String, BodyPublisher) is almost always unintentional,
> it may be helpful to include a similar message for these methods as well.
>
> For example,
> requireNonNull(body, "BodyPublisher must be non-null");
>
> Adding such a message would improve clarity and consistency
> with other null checks already present in this class.
>
> I have searched for existing issues related to this behavior
> but was unable to find any.
> If I have overlooked something or if this suggestion is inappropriate,
> please feel free to let me know.
> If the proposal sounds reasonable,
> I would be happy to prepare a patch and contribute it.
>
> Thank you for your time and consideration.
> Best regards,
> Hyunsu Eun
> GitHub: https://github.com/ehs208
More information about the net-dev
mailing list