Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is ObjectUtils.requireNonEmpty() in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

requireNonEmpty() is a staticthe methods in Java that can be called without creating an object of the class. method of the ObjectUtils class that is used to check whether a specified object reference is not null or not empty according to the isEmpty() method. If the specified object reference is either null or empty, the method throws an exception with the specified message.

How to import ObjectUtils

The definition of ObjectUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the ObjectUtils class as follows:


import org.apache.commons.lang3.ObjectUtils;

Syntax

public static <T> T requireNonEmpty(T obj, String message)

Parameters

  • obj: The object to check of type T.
  • message: The exception message if the object is null or empty.

Return value

This method returns the input object if it is not null.

Code

import org.apache.commons.lang3.ObjectUtils;
public class Main{
public static void main(String[] args) {
// Example 1
String object = "hello";
System.out.printf("ObjectUtils.requireNonEmpty(%s) = %s", object, ObjectUtils.requireNonEmpty(object));
System.out.println();
// Example 2
object = "";
String message = "Object must not be empty/null";
System.out.printf("ObjectUtils.requireNonEmpty(%s) = %s", object, ObjectUtils.requireNonEmpty(object, message));
System.out.println();
}
}

Output

The output of the code will be as follows:


ObjectUtils.requireNonEmpty(hello) = hello
Exception in thread "main" java.lang.IllegalArgumentException: Object must not be empty/null
	at org.apache.commons.lang3.ObjectUtils.requireNonEmpty(ObjectUtils.java:1241)
	at Main.main(Main.java:12)

Explanation

Example 1

  • object = "hello"

The method returns hello because the object is neither empty nor null.

Example 2

  • object = ""

The method throws an IllegalArgumentException with the exception message, as Object must not be empty/null.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring