java.lang.NullPointerException

The java.lang.NullPointerException occurs when a java variable does not point to any object and is accessed for any operation. The java.lang.NullPointerException is a runtime exception. The java.lang.NullPointerException is an exception to runtime. The java NullPointerException occurs when a variable is declared but is not assigned to an object until you try to use the variable. You’re referring to something that doesn’t exist.

Null represents a special value in Java. It is primarily used to mean that a reference variable is not assigned to a value or no value. In order to avoid NullPointerException, we must make sure that all objects are correctly initialised before the variable is used.



Exception

Identifying the NullPointerException is quite simple. The NullPointerException print stack trace will display the exception class name and line number. Then look at the code to see what could be null that causes the NullPointerException.

Exception in thread "main" java.lang.NullPointerException
	at com.yawintutor.TestNullPointerException.main(TestNullPointerException.java:8)


Root Cause

The java.lang.NullPointerException occurs in the program when you perform a java variable operation that is assigned to null. There are some common reasons to throw java.lang.NullPointerException in Java program.

Calling the instance method of a null object.
Accessing or modifying the field of a null object.
Taking the length of null as if it were an array.
Accessing or modifying the slots of null as if it were an array.
Throwing null as if it were a Throwable value.



How to reproduce java.lang.NullPointerException

If a java variable is created that does not point to an object and is used to perform some operation, the NullPointerException will be thrown. The example below tries to find the length of the string assigned to null.

package com.yawintutor;

public class TestNullPointerException {
	public static void main(String[] args) {
		String str = null;
		int length = 0;
		
		length = str.length();

		System.out.println("Given String  : " + str);
		System.out.println("String Length : " + length);
	}
}

Output

Exception in thread "main" java.lang.NullPointerException
	at com.yawintutor.TestNullPointerException.main(TestNullPointerException.java:8)


Solution 1

Identify the java variable that throws java.lang.NullPointerException in the program, make sure the variable is assigned to a value or object. If a value is assigned to the java variable, the java.lang.NullPointerException will be discarded.

package com.yawintutor;

public class TestNullPointerException {
	public static void main(String[] args) {
		String str = "Yawin";
		int length = 0;
		
		length = str.length();

		System.out.println("Given String  : " + str);
		System.out.println("String Length : " + length);
	}
}

Output

Given String  : Yawin
String Length : 5


Solution 2

If a java.lang.NullPointerException is thrown into a variable, use the ternary operator to set the default value before calling the java variable to perform the operation. If the variable contains a value, the value will be used. Otherwise, the default value would be used to prevent java.lang.NullPointerException.

package com.yawintutor;

public class TestNullPointerException {
	public static void main(String[] args) {
		String str = null;
		int length = 0;
		
		str = str==null?"":str;
		length = str.length();

		System.out.println("Given String  : " + str);
		System.out.println("String Length : " + length);
	}
}

Output

Given String  : 
String Length : 0


Solution 3

If the java variable throws java.lang.NullPointerException in the program, do a null check before using the variable. If the variable is null, take the alternate path. This will discard the java.lang.NullPointerException.

package com.yawintutor;

public class TestNullPointerException {
	public static void main(String[] args) {
		String str = null;
		int length = 0;
		
		if(str==null) {
			length = 0;
		} else {
			length = str.length();
		}
		
		System.out.println("Given String  : " + str);
		System.out.println("String Length : " + length);
	}
}

Output

Given String  : null
String Length : 0


Solution 4

If you’re not sure which variable is throwing java.lang.NullPointerException or if there’s a risk of throwing java.lang. NullPointerException at multiple locations, it’s tedious to add null check to the code. Then add a catch block attempt to tackle the java.lang.nullPointerException.

package com.yawintutor;

public class TestNullPointerException {
	public static void main(String[] args) {
		String str = null;
		int length = 0;
		
		try {
			length = str.length();
		} catch (NullPointerException e) {
			length = 0;
		}

		System.out.println("Given String  : " + str);
		System.out.println("String Length : " + length);
	}
}

Output

Given String  : null
String Length : 0


Solution 5

If you try to find the length of the array assigned to null, the java.lang.nullPointerException will be thrown. If an empty array is assigned to an array variable, the exception will be resolved.

package com.yawintutor;

public class TestNullPointerException {
	public static void main(String[] args) {
		String[] str = null;
		int length = 0;
		
		length = str.length;

		System.out.println("Given String  : " + str);
		System.out.println("String Length : " + length);
	}
}

Solution

package com.yawintutor;

public class TestNullPointerException {
	public static void main(String[] args) {
		String[] str = new String[0];
		int length = 0;
		
		length = str.length;

		System.out.println("Given String  : " + str);
		System.out.println("String Length : " + length);
	}
}

Output

Given String  : [Ljava.lang.String;@7852e922
String Length : 0



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *