//java.lang.NullPointerException
Invoking 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.
When you add null to Collections which do not allow nulls such as ConcurrentHashMap
Trying to synchronize using null object.
package govind;import java.util.concurrent.ConcurrentHashMap;
public class Customer {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
//case:1
/*Customer c1=null;
String s1=c1.getName();
System.out.println("Customer Name: "+s1);*/
//case:2
/*Customer c2=null;
String s1 = c2.name;
System.out.println("Customer Name: "+ s1);*/
//case:3
/*Customer []c=null;
int x=c.length;
System.out.println("length="+x);*/
//case:4
/*Customer c1[]=null;
System.out.println(c1[3]);*/
//case:5
/* throw null;*/
//case:6
/*ConcurrentHashMap<String, String> chm=new ConcurrentHashMap<>();
chm.put(null, "govind");*/
//case:7
/*Object obj=null;
synchronized (obj) {
ConcurrentHashMap<String, String> map=new ConcurrentHashMap<>();
map.put("govind", "khan");
}*/
}
}
=============================================================================================================================================
o/p ........................
Null pointer exception
No comments:
Post a Comment