Check Array Is Null or Empty Example
1. Introduction
An array is a data structure that holds a fixed number of elements with the same data type. In Java, an array object can be null. When an array is created for object types, e.g. String, Integer, Long, or any other custom data class, the default value for each element is null
unless explicitly initialized. To prevent a NullPointerException, we should do the null
check before accessing an object. In this example, I will demonstrate an array check null or empty in a Java example.
2. Setup
In this step, I will create a maven project with both Junit5 and Apache Commons Libraries.
pom.xml
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >Check_Array_Demo</ groupId > < artifactId >Check_Array_Demo</ artifactId > < version >0.0.1-SNAPSHOT</ version > < build > < sourceDirectory >src</ sourceDirectory > < plugins > < plugin > < artifactId >maven-compiler-plugin</ artifactId > < version >3.8.1</ version > < configuration > < release >17</ release > </ configuration > </ plugin > </ plugins > </ build > < dependencies > < dependency > < groupId >org.junit.jupiter</ groupId > < artifactId >junit-jupiter-api</ artifactId > < version >5.11.0</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.junit.jupiter</ groupId > < artifactId >junit-jupiter-engine</ artifactId > < version >5.11.0</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.apache.commons</ groupId > < artifactId >commons-lang3</ artifactId > < version >3.17.0</ version > </ dependency > </ dependencies > </ project > |
3. Array Check Null or Empty
In this step, I will create a CheckArrayUtils.java
class to check if a given int
or String
array is empty or null
.
CheckArrayUtils.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 | package org.zheng.demo; public class CheckArrayUtils { public boolean isEmpty( int [] intArray) { return intArray == null || intArray.length == 0 ; } public boolean isEmpty(String[] stringArray) { return stringArray == null || stringArray.length == 0 ; } public boolean isNull( int [] intArray) { return intArray == null ; } public boolean isNull(String[] stringArray) { return stringArray == null ; } } |
In order to show the test method’s execution time in nanoseconds, I will create a NanosecondsTimeExtension.java
which prints out the execution time in nanoseconds.
NanosecondsTimeExtension.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package org.zheng.demo; import org.junit.jupiter.api.extension.AfterTestExecutionCallback; import org.junit.jupiter.api.extension.BeforeTestExecutionCallback; import org.junit.jupiter.api.extension.ExtensionContext; public class NanosecondsTimeExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback { private static final String START_TIME = "startTime" ; @Override public void afterTestExecution(ExtensionContext context) throws Exception { ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.GLOBAL); long startTime = store.remove(START_TIME, long . class ); long endTime = System.nanoTime(); long execTimeInNS = (endTime - startTime); System.out.println(context.getDisplayName() + " took " + execTimeInNS + " Nanoseconds" ); } @Override public void beforeTestExecution(ExtensionContext context) throws Exception { ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.GLOBAL); store.put(START_TIME, System.nanoTime()); } } |
4. Test Int Array Check Null or Empty
In this step, I will create a TestCheckIntArray.java
class which has five test methods to check array null
or empty for an integer array via ArrayUtils
, ObjectUtils
, and the native Java language.
TestCheckIntArray
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package org.zheng.demo; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.stream.IntStream; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ObjectUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @ExtendWith (NanosecondsTimeExtension. class ) class TestCheckIntArray { int [] emptyArray = {}; int [] intArray = IntStream.generate(() -> 1000 ).limit( 1000 ).toArray(); int [] nullArray = null ; private CheckArrayUtils testClass = new CheckArrayUtils(); @Test void test_ApacheArrayUtils_isEmpty() { assertTrue(ArrayUtils.isEmpty(nullArray)); assertTrue(ArrayUtils.isEmpty(emptyArray)); assertFalse(ArrayUtils.isEmpty(intArray)); } @Test void test_ApacheObjectUtils_isEmpty() { assertTrue(ObjectUtils.isEmpty(nullArray)); assertTrue(ObjectUtils.isEmpty(emptyArray)); assertFalse(ObjectUtils.isEmpty(intArray)); } @Test void test_emptyCheck() { assertTrue(testClass.isEmpty(nullArray)); assertTrue(testClass.isEmpty(emptyArray)); assertFalse(testClass.isEmpty(intArray)); } @Test void test_nullCheck() { assertTrue(testClass.isNull(nullArray)); assertFalse(testClass.isNull(emptyArray)); assertFalse(testClass.isNull(intArray)); } @Test void test_nullPointerException() { assertThrows(NullPointerException. class , () -> { System.out.println( " emptyArray size is " + emptyArray.length); System.out.println( " never print out due to NullPointerException" + nullArray.length); }); } } |
- Line 23:
test_ApacheArrayUtils_isEmpty
checks if an array is empty via ApacheArrayUtils.isEmpty
method. - Line 30:
test_ApacheObjectUtils_isEmpty
checks if an array is empty via ApacheObjectUtils.isEmpty
method. - Line 37:
test_emptyCheck
checks if an array is empty viatestClass.isEmpty
method. - Line 44:
test_nullCheck
checks if an array isnull
viatestClass.isNull
method. - Line 51:
test_nullPointerException
verifiesNullPointerException
is thrown when accessingnull
.
Execute the test and capture the output:
TestCheckIntArray Results
1 2 3 4 5 6 | test_ApacheObjectUtils_isEmpty() took 14467400 Nanoseconds emptyArray size is 0 test_nullPointerException() took 4456900 Nanoseconds test_nullCheck() took 232100 Nanoseconds test_ApacheArrayUtils_isEmpty() took 4904600 Nanoseconds test_emptyCheck() took 203000 Nanoseconds |
- Line 4, 6 – the native
null
and empty check methods have better performance than ApacheObjectUtils
andArrayUtils
methods.
5. Test String Array Check Null or Empty
In this step, I will create a TestCheckStringArray.java
class which is similar to the TestCheckIntArray.java
created in step 4.
TestCheckStringArray.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package org.zheng.demo; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ObjectUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @ExtendWith (NanosecondsTimeExtension. class ) class TestCheckStringArray { String[] emptyArray = {}; String[] letterArray = { "a," , "b" , "C" , "d" , "Z" }; String[] nullArray = null ; private CheckArrayUtils testClass = new CheckArrayUtils(); @Test void test_ApacheArrayUtils_isEmpty() { assertTrue(ArrayUtils.isEmpty(nullArray)); assertTrue(ArrayUtils.isEmpty(emptyArray)); assertFalse(ArrayUtils.isEmpty(letterArray)); } @Test void test_ApacheObjectUtils_isEmpty() { assertTrue(ObjectUtils.isEmpty(nullArray)); assertTrue(ObjectUtils.isEmpty(emptyArray)); assertFalse(ObjectUtils.isEmpty(letterArray)); } @Test void test_emptyCheck() { assertTrue(testClass.isEmpty(nullArray)); assertTrue(testClass.isEmpty(emptyArray)); assertFalse(testClass.isEmpty(letterArray)); } @Test void test_nullCheck() { assertTrue(testClass.isNull(nullArray)); assertFalse(testClass.isNull(emptyArray)); assertFalse(testClass.isNull(letterArray)); } @Test void test_nullPointerException() { assertThrows(NullPointerException. class , () -> { System.out.println( " emptyArray size is " + emptyArray.length); System.out.println( " never print out due to NullPointerException" + nullArray.length); }); } } |
Execute the test and capture the output:
TestCheckStringArray Results
1 2 3 4 5 6 | test_ApacheObjectUtils_isEmpty() took 12847300 Nanoseconds emptyArray size is 0 test_nullPointerException() took 5099400 Nanoseconds test_nullCheck() took 199900 Nanoseconds test_ApacheArrayUtils_isEmpty() took 4829600 Nanoseconds test_emptyCheck() took 138100 Nanoseconds |
Capture both tests’ results with the following screenshot.
6. Conclusion
In this example, I created a simple Java class to check if a given integer or String
array is null
or empty. I tested the methods with Junit as well as the ObjectUtils.isEmpty
and ArrayUtils.isEmpty
methods from Apache Common Library. As you can see from the test results, the native methods have better performance.
7. Download
This was an example of a maven project which includes an array check null
or empty features.
You can download the full source code of this example here: Check Array Is Null or Empty Example