Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Validate.noNullElements() 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.

noNullElements() is a staticthe methods in Java that can be called without creating an object of the class. method of the Validate class that is used to check whether a given array or an iterable is neither null nor contains any elements that are null. Otherwise, the method throws an exception with the specified message.

How to import Validate

The definition of Validate 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 Validate class as follows:

import org.apache.commons.lang3.Validate;

Syntax

public static <T> T[] noNullElements(final T[] array, final String message, final Object... values)

Parameters

  • final T[] array: The array to check.

  • final String message: The exception message.

  • final Object... values: The optional values for the formatted exception message.

Return value

This method throws an exception if the input array is or contains null elements. Otherwise, it returns the input array.

Overloaded methods

  • public static <T> T[] noNullElements(final T[] array)
  • public static <T extends Iterable<?>> T noNullElements(final T iterable, final String message, final Object... values)
  • public static <T extends Iterable<?>> T noNullElements(final T iterable)

Code

import org.apache.commons.lang3.Validate;

import java.util.Arrays;

public class Main{

    public static void main(String[] args){
        // Example 1
        Integer[] ints = new Integer[]{1, 2, 3};
        System.out.printf("Validate.noNullElements(%s) = %s", Arrays.toString(ints), Arrays.toString(Validate.noNullElements(ints)));
        System.out.println();

        // Example 2
        ints = new Integer[]{1, 2, null};
        String exceptionMessage = "Array should not have any null elements";
        System.out.printf("Validate.noNullElements(%s) = %s", Arrays.toString(ints), Arrays.toString(Validate.noNullElements(ints, exceptionMessage)));
        System.out.println();
    }
}

Output

The output of the code will be as follows:

Validate.noNullElements([1, 2, 3]) = [1, 2, 3]
Exception in thread "main" java.lang.IllegalArgumentException: Array should not have any null elements
	at org.apache.commons.lang3.Validate.noNullElements(Validate.java:500)
	at Main.main(Main.java:16)

Example 1

  • array to check - [1, 2, 3]

The method returns the input array [1, 2, 3], as the array is not null and does not have any null elements.

Example 2

  • array to check - [1, 2, null]

The method throws an IllegalArgumentException exception with the exception message Array should not have any null elements as the array contains null elements.

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