All About Class NullPointerException in Java

NullPointerException is thrown from an application when our code attempts to use null in the case where an object is required.
Below can be the scenarios where NullPointerException can occur:

  1. Calling the instance method of the/a null object.
  2. Accessing or modifying the field of the null object.
  3. Tracking the length of null as if it was empty.
  4. Accessing or modifying the slots of null as if it was an array.
  5. Throwing null as if there was a Throwable value
  6. Attempt to access a field of null object
  7. Attempt to invoke a method of a null object.
Applications should throw instances of this class to indicate other illegal uses of the null object.
The structure of NullPointerException class is given as :

public class java.lang.NullPointerException extends java.lang.RuntimeException{
public NullPointerException();
//constructs an empty NullPointerException object that is an object with no message specified.
public NullPointerException(String message);
//constructs an NullPointerException object with the message specified.

So when an attempt to use a method or a variable that contains a null object reference,this NullPointerException occurs.

public void myMethod(){
String myString=null;
output of the code:
All About Class NullPointerException in Java All About Class NullPointerException in Java Reviewed by Animesh Chatterjee on September 18, 2018 Rating: 5

No comments:

Powered by Blogger.