Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me what is null pointer exception? And why it occurs?

1 Answer

0 votes
by (26.7k points)

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

   }

Output:

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

Browse Categories

...