Null Pointer Exception in JavaIn 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 ExceptionObserve some of the following scenarios where NullPointerException can be raised.
FileName: ThrowNullExcep.java Output: Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "
FileName: ThrowNullExcep1.java Output: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "ThrowNullExcep1.foo()" because "
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)
FileName: ThrowNullExcep3.java Output: Exception in thread "main" java.lang.NullPointerException: Cannot assign field "a" because " Requirement of NULL ValueA 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 NullPointerExceptionIn 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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India