Introduce to 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.
A NullPointerException is thrown when there is an attempt to access an object through a null object reference. This can occur when there is an attempt to access an instance variable or call a method through a null object or when there is an attempt to subscript an array with a null object.
Below can be the scenarios where NullPointerException can occur:
- Calling the instance method of the/a null object.
- Accessing or modifying the field of the null object.
- Tracking the length of null as if it was empty.
- Accessing or modifying the slots of null as if it was an array.
- Throwing null as if there was a Throwable value
- Attempt to access a field of the null object
- 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{
//constructor
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.
The class NullPointerException also inherits methods from class Object and Throwable.
From Object class | From Throwable class |
---|---|
clone() | fillInStackTrace() |
getClass() | getMessage() |
notify() | printStackTrace() |
wait() | printStackTrace(PrintWriter) |
wait(long, int) | getLocalizedMessage() |
wait(long) | printStackTrace(PrintStream) |
equals(Object) | |
toString() | |
finalize() | |
hashCode() | |
notifyAll() |
Example Of NullPointerException
public void myMethod(){
String myString=null;
if(myString.equals("abc")){
System.out.println("Hello");
}
}
the output of the code:
NullPointerException
Click here to know more.
Raise NullPointerException on your own
Class MyTestclass {
int number;
void setNumber(int number)
if(number>10)
this.number= number;
else {
NullPointerException exp= new NullPointerException("The number is not big enough");
exp.initCause(new ArithmeticException("the number is invalid"));
throw exp;
}
}
public static void main (string args[ ]){
MyTestclass mtc= new MyTestclass( );
try {
mtc.setNumber(20);
mtc.setNumber(5);
}
catch(NullPointerException exp)
{
system.out.printh(e.getMessage( ));
system.out.printh(e.getMessage( ));
}
}
Read more:what-is-interface-in-java
The top-level exception null pointer exception has added a cause of the exception, the Arithmetic exception.
When it is actually caught by the main ( ) method, the top-level exception is shown followed by the underlying exception. This underlying exception is obtained by the method getcause ( ).
Chain exception is possible to unlimited depth. However, long-chain exception is a sign of poor code design.