Monday, 19 March 2012

NullPointerException in Java with Examples


public class NullPointerException
 extends RuntimeException

Thrown when an application attempts to use null in a case where an object is required. These include:
  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.
Applications should throw instances of this class to indicate other illegal uses of the null object.

Examples for NullPointerException

1) Calling the instance method of a null object.

public class Main {

public static void main(String args[]) {

String s = null;
System.out.println(s.length());
}
}

The output is

Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:8)


2) Accessing or modifying the field of a null object.

public class Main {

public static void main(String args[]) {

Student s = null;
System.out.println(s.rollno);

}

}

class Student {
int rollno;
String name;
String address;
}

The output is

Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:6)


3) Taking the length of null as if it were an array.

public class Main {

public static void main(String args[]) {

int arr[] = null;
System.out.println(arr.length);

}

}

The output is

Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:6)

4) Accessing or modifying the slots of null as if it were an array.

public class Main {

public static void main(String args[]) {

int arr[] = null;
System.out.println(arr[0]);

}

}

The output is

Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:6)

5) Throwing null as if it were a Throwable value.

public class Main {

public static void main(String args[]) throws Exception {

Exception myException =null;
throw myException;

}
}

The output is

Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:7)

How to avoid NullPointerException

To avoid the possibility of NullPointerException always use a null check before performing any operations with an object reference.See one exception

public class Main {

public static void main(String args[]){

String s = null;
if (s != null)
System.out.println("String s length is: " + s.length());

}

}

How to Debug NullPointerException

When you see a null pointer exception. You should go to the line specified in the error and check if there might be any null references there. To do that you can add basic "if statements" for your object references used around that line (just like in the above example). Once you found the null reference variable, you can fix the problem by changing the code there.

How to Throw User Defined NullPointerException

Suppose you want to override the equals method in such a way that it throws NullPointerException when its argument is null.See the implementation below.

public class Main {

public static void main(String args[]) throws Exception {

Student s1 = new Student(10, "John");
Student s2 = null;

System.out.println(s1.equals(s2));

}

}

class Student {
int rollno;
String name;

Student(int rollno, String name) {
this.rollno = rollno;
this.name = name;
}

boolean equals(Student obj) {
if (obj == null)
throw new NullPointerException("Comparing object is null");
return (rollno == obj.rollno && name.equals(obj.name));
}
}

The output is

Exception in thread "main" java.lang.NullPointerException: Comparing object is null
at Student.equals(Main.java:25)
at Main.main(Main.java:8)

No comments:

Post a Comment