Javatpoint Logo
Javatpoint Logo

Null Pointer Exception in Java

In this tutorial, we are going to learn the Null pointer exception in Java. Null pointer exception is a runtime exception. Null is a special kind of value that can be assigned to the reference of an object. Whenever one tries to use a reference that has the Null value, the NullPointerException is raised.

Different Scenarios for the Null Pointer Exception

Observe some of the following scenarios where NullPointerException can be raised.

  • Computing the size or length of the Null as if it were an array of elements.

FileName: ThrowNullExcep.java

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "" is null at ThrowNullExcep.main(ThrowNullExcep.java:7)
  • Calling a method using the object that has the Null value.

FileName: ThrowNullExcep1.java

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "ThrowNullExcep1.foo()" because "" is null
	at ThrowNullExcep1.main(ThrowNullExcep1.java:13)
  • When you try to synchronize over a NULL object.

FileName: ThrowNullExcep2.java

Output:

Exception in thread "Thread-0" Exception in thread "Thread-1" java.lang.NullPointerException: Cannot enter synchronized block because "this.sendr" is null
	at ThreadSend.run(ThrowNullExcep2.java:42)
java.lang.NullPointerException: Cannot enter synchronized block because "this.sendr" is null
	at ThreadSend.run(ThrowNullExcep2.java:42)
  • Instead of throwing a value, Null is thrown.

FileName: ThrowNullExcep3.java

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot assign field "a" because "" is null
	at ThrowExcep3.main(ThrowExcep3.java:10)

Requirement of NULL Value

A null is a special value that is used in Java. It is usually used to show that there is no value that is being assigned to the reference variable. A null value is mainly used in the implementation of data structures such as a linked list or tree. It is also used in the Singleton pattern.

Avoiding the NullPointerException

In order to avoid the NullPointerException, one should ensure that the initialization of all of the objects is done properly before one can use them. When one declares a reference variable, one should verify that the null value is not assigned to the reference before the reference value is used to access the field or method.

Observe the following common problems with the solution.

Case 1: Comparison of strings with the literal

One of the common problems includes the comparison of a literal with a String variable. The literal can be an element from an Enum or from a String. Instead of invoking the method from the null object, consider invoking it using the literal.

FileName: NullPntrExcption.java

Output:

NullPointerException has been caught.

Now, let's see how one can avoid it.

FileName: NullPntrExcption1.java

Output:

NullPointerException has been caught.

Case 2: Keeping an Eye on the method arguments

One must check the method arguments first for the null values and then go with the method execution. Otherwise, there are fair chances that it will throw an IllegalArgumentException and will also signal the calling method that it is wrong with the arguments that have been passed.

FileName: NullPntrExcption2.java

Output:

0
3
IllegalArgumentException has been caught. java.lang.IllegalArgumentException: The argument can never be null.

Case 3: Use of Ternary Operator

One can also use the ternary operator in order to avoid the NullPointerException. In ternary, the Boolean expression gets evaluated first. If the expression is evaluated as true, then the val1 is returned. Else, the val2 is returned.

FileName: NullPntrExcption3.java

Output:

String is Null.
javaTpoint






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA