What is a Null Pointer Exception? |
Thrown when an application attempts to use null in a case where an object is required. These are the common scenarios where one can confront with a null pointer exception:-
–
Package Structure:
ava.lang.Object—>java.lang.Throwable—>java.lang.Exception—>
java.lang.RuntimeException—>java.lang.NullPointerException
Example,
String a = null;
System.out.println(a.toString());
Handling Null Pointer Exception,
public void method(Object object) {
if (object == null) {
throw new IllegalArgumentException(“Null Values not allowed”);
}
}
int a=5; /* avoid use of new Integer(5)*/
public student matchCriteria(){
return new Student(;)/* avoid return null; */
}
“Sandeep”.equal (inputname).
If you look into the src folder of jdk the then you can see the definition of this NullPointerException
public class NullPointerException extends RuntimeException {
public NullPointerException() {
super();
}
public NullPointerException(String s) {
super(s);
}
}
NullPointerException object is a type of RuntimeException.
|
Java : Null Pointer Exception
Sandeep
A passionate Blogger and Developer.Love to code for web application using JavaScript.At present I am exploring the Web Component Specification.