Back
Basically, NullPointerException is a runtime exception and it occurs whenever an object reference has a null value. Below is an example :
Input:
public class Intellipaat { public static void main(String[] args) { Object ref = null; ref.toString(); // this will throw a NullPointerException }}
public class Intellipaat {
public static void main(String[] args) {
Object ref = null;
ref.toString(); // this will throw a NullPointerException
}
Output:
Exception in thread "main" java.lang.NullPointerExceptionat Tester.main(Tester.java:4)
Exception in thread "main" java.lang.NullPointerException
at Tester.main(Tester.java:4)
I hope this will help.
Want to become a Java expert? join Java Course now!!
31k questions
32.8k answers
501 comments
693 users