NullPointerException in Java is a unchecked Exception defined inwards java.lang bundle in addition to comes when a fellow member of a an object either champaign or method is called on a object which is null. zero is a keyword inwards Java which way nada in addition to calling method on Object whose value is null volition resultant inwards NullPointerException. Since default value of Object is null, if yous telephone phone whatever method or access whatever champaign on an Object which is non initialized volition throw NullPointerException. Some programmer acquire confused amongst cite equally well, Since Java does non back upward pointers, How tin yous accept NullPointerException in Java? Well java.lang.NullPointerException doesn't accept anything to produce amongst pointers, it only an Exception inwards Java. If yous hold off at it to a greater extent than deeply, NullPointerException is an unchecked Exception in addition to it's non mandatory to render Exception treatment code for it using try, pick out handgrip of in addition to finally. In fact virtually of NullPointerException comes because of programming errors. See virtually common drive of NullPointerException to discovery out virtually mutual scenario where NullPointerException comes inwards Java. In this Java article nosotros volition non exclusively encounter What is NullPointerException but too How to avoid NullPointerException and How to create java.lang.NullPointerException in Java.
When NullPointerException occurs inwards Java
In in conclusion department nosotros saw that What is java.lang.NullPointerException and finds that NullPointerException occurs when nosotros telephone phone method or access members on an Object which is null, i.e. either its non initialized or it has been ready zero explicitly. This is key of NullPointerException but it manifest itself inwards many places e.g.
1) NullPointerException comes when yous endeavour to purpose an Object inwards synchronized keyword which is zero equally shown inwards next illustration :
Object lock = null;
synchronized(lock){ } //NullPointerException, tin non synchronized amongst zero object
synchronized(lock){ } //NullPointerException, tin non synchronized amongst zero object
2) Similarly if yous endeavour to access whatever chemical component from Array, which is zero volition resultant inwards NullPointerException.
How to solve NullPointerException in Java
Once yous empathize that NullPointerException occurs because of zero object, yesteryear looking at stack line of NullPointerException. Since virtually common drive of NullPointerException is that reference variable pointing to zero instead of an valid Object reference. Let's encounter an illustration of NullPointerException in Java :
/**
*
* Java programme to demonstrate When NullPointerException occurs and
* How to create NullPointerException inwards Java
*
* @author java67
*/
public class Hello {
private static String name;
public static void main(String args[]){
if(name.equals("Java")){
System.err.println("Welcome to Java");
}
}
}
Exception inwards thread "main" java.lang.NullPointerException
at test.CollectionTest.main(CollectionTest.java:18)
*
* Java programme to demonstrate When NullPointerException occurs and
* How to create NullPointerException inwards Java
*
* @author java67
*/
public class Hello {
private static String name;
public static void main(String args[]){
if(name.equals("Java")){
System.err.println("Welcome to Java");
}
}
}
Exception inwards thread "main" java.lang.NullPointerException
at test.CollectionTest.main(CollectionTest.java:18)
Now yesteryear looking output in addition to stack line nosotros know that fault occurred at line of piece of employment 18, where nosotros are comparison cite member variable to String literal "Java" e.g. name.equals("Java"), Since cite is non initialized anywhere inwards programme in addition to its an String Object, it contains default value null, which is causing this NullPointerException . To avoid this NullPointerException we tin produce ii things
1) start produce a zero cheque e.g. name != null earlier calling whatever method on that
2) nosotros tin purpose the fact that equals method render simulated if nosotros compare it volition zero e.g. if nosotros write code similar "java".equals(name) nosotros volition non acquire whatever NullPointerException, instead it volition return false.
In this Java tutorial nosotros larn What is NullPointerException in Java, When does NullPointerException occurs inwards Java in addition to How to create NPE inwards Java. In Summary endeavour to avoid NullPointerException by making certain yous follow the contract e.g. Using object when its initialized in addition to yesteryear defensive coding. You tin too write zero prophylactic method likes equals() if yous don't trust your customer to avoid NullPointerException in Java.
Further Learning
Complete Java Masterclass
What is divergence betwixt abstraction in addition to encapsulation inwards Java
No comments:
Post a Comment