NullPointerException class is available in java.lang
package and it extends RuntimeException
class
public class NullPointerException extends RuntimeException
NullPointerException
class inherits methods from java.lang.Object
class and java.lang.Throwable class.
Hierarchy of NullPointerException
When NullPointerException throw
NullPointerException
throw when a method called by null object.
For example,
calling length() method on null string
String name=null;
System.out.println(name.length());//NullPointerException
Example:
package com.exceptions;
public class
NullPointerExceptionTest {
void message(){
System.out.println("Hello
World");
}
public static void main(String[] args) {
NullPointerExceptionTest
obj=null;
obj.message();
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
at
com.exceptions.NullPointerExceptionTest.main(NullPointerExceptionTest.java:8)
Internal code of NullPointerException from java
api
package java.lang;
public class NullPointerException
extends RuntimeException
{
private static final long serialVersionUID = 5162710183389028792L;
public NullPointerException() {}
public NullPointerException(String paramString)
{
super(paramString);
}
}
It has no
argument constructor and one String argument constructor.
No comments:
Post a Comment