JDK-8373660 : Add explicit null checks using Objects.requireNonNull
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Priority: P4
  • Status: New
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2025-12-14
  • Updated: 2025-12-15
Description
ADDITIONAL SYSTEM INFORMATION :
Operating System: darwin 24.6.0 (macOS)
JDK Version: openjdk version "21" 2023-09-19 LTS
Build: OpenJDK Runtime Environment (build 21+35-LTS)
Architecture: x86_64

Additional Information:
- Working on OpenJDK JDK repository
- Testing with jtreg test framework

A DESCRIPTION OF THE PROBLEM :
I would like to contribute to OpenJDK by adding explicit null 
checks using Objects.requireNonNull to improve defensive 
programming and provide clearer error messages.

Motivation:
Some public API methods currently throw NullPointerException 
when null is passed, but the error message is not clear. Using 
Objects.requireNonNull provides a clearer error message and 
makes the API contract explicit.

Proposed Changes:
- Add Objects.requireNonNull check in DataOutputStream.writeBytes(String s)
- This will provide a clear error message when null is passed

Target File:
src/java.base/share/classes/java/io/DataOutputStream.java

Testing:
I will add test cases to verify that null checks work correctly 
and provide appropriate error messages.

This is my first contribution to OpenJDK. I'm ready to submit 
a pull request once a JBS issue is created.



Comments
And it would arguably be useful if the implementation of Objects.requireNonNull threw the more helpful NPE messages, possibly with a JVM assist.
15-12-2025

The API contract should be explicit already - java.io package specifies NPE by default.
15-12-2025

Since JDK 14, and JEP 358: Helpful NullPointerExceptions, the exception message when null is dereferenced can be useful. The example in this JBS issue is DataOutputStream.writeBytes(null) where, since JDK 14, the exception message will be "Cannot invoke "String.length()" because "s" is null". So I think best to send the submitter to core-libs-dev to discuss specific methods that they propose to change so that they can be discussed.
14-12-2025