DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Jakarta NoSQL: Why JPA Is Not Enough for the AI Era
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java
  • A Spring Boot App With Half the Startup Time
  • Implementing the Planning Pattern With Java Enterprise and LangChain4j

Trending

  • Amazon CodeWhisperer to Q Developer to Kiro: The Rise of Agentic Coding
  • A Deep Dive into Tracing Agentic Workflows (Part 2)
  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • A Spring Boot App With Half the Startup Time
  1. DZone
  2. Coding
  3. Java
  4. Checking for Null Values in Java with Objects.requiresNonNull()

Checking for Null Values in Java with Objects.requiresNonNull()

By 
Michael Scharhag user avatar
Michael Scharhag
·
Oct. 03, 14 · Interview
Likes (2)
Comment
Save
Tweet
Share
65.9K Views

Join the DZone community and get the full member experience.

Join For Free

Checking method/constructor parameters for null values is a common task problem in Java. To assist you with this, various Java libraries provide validation utilities (see Guava Preconditions, Commons LangValidate or Spring's Assert documentation).
However, if you only want to validate for non null values you can use the static requiresNonNull()method of java.util.Objects. This is a little utility introduced by Java 7 that appears to be rarely known.

With Objects.requiresNonNull() the following piece of code

public void foo(SomeClass obj) {
	if (obj == null) {
		throw new NullPointerException("obj must not be null");
	}
	// work with obj
}

can be replaced with:

import java.util.Objects;

public void foo(SomeClass obj) {
	Objects.requireNonNull(obj, "obj must not be null");
	// work with obj
}


Java (programming language)

Published at DZone with permission of Michael Scharhag. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta NoSQL: Why JPA Is Not Enough for the AI Era
  • Top Java Security Vulnerabilities and How to Prevent Them in Modern Java
  • A Spring Boot App With Half the Startup Time
  • Implementing the Planning Pattern With Java Enterprise and LangChain4j

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook