- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What happens if NullPointerException is not handled in main method of java?
In Java there is default value for every type, when you don’t initialize the instance variables of a class Java compiler initializes them on your be-half with these values. Null is the default value of the object type, you can also manually assign null to objects in a method.
Object obj = null;
But, you cannot use an object with null value or (a null value instead of an object) if you do so, a NullPointerException will be thrown.
Following are some scenarios where NullPointerException occurs.
- Calling the a method (instance) using null object.
public class Demo { public void demoMethod() { System.out.println("Hello how are you"); } public static void main(String args[]) { Demo obj = null; obj.demoMethod(); } }
Run time exception
Exception in thread "main" java.lang.NullPointerException at july_set3.Demo.main(Demo.java:11)
- Accessing, modifying, printing, field of a null value(object).
public class Demo { String name = "Krishna"; int age = 25; public static void main(String args[]) { Demo obj = null; System.out.println(obj.age); System.out.println(obj.name); } }
Run time exception
Exception in thread "main" java.lang.NullPointerException at july_set3.Demo.main(Demo.java:11)
- Trying to access (print/use in statements) the length of null value.
public class Demo { String name = null; public static void main(String args[]) { Demo obj = new Demo(); System.out.println(obj.name.length()); } }
Run time exception
Exception in thread "main" java.lang.NullPointerException at july_set3.Demo.main(Demo.java:7)
- Throw a null value.
public class Demo { public static void main(String args[]) { throw null; } }
Runtime exception
Exception in thread "main" java.lang.NullPointerException at july_set3.Demo.main(Demo.java:5)
- Accessing or modifying elements/slots of a null value.
public class Demo { public static void main(String args[]) { int myArray[] = null; System.out.println(myArray[5]); } }
Runtime exception
Exception in thread "main" java.lang.NullPointerException at july_set3.Demo.main(Demo.java:6)
Avoiding NullPointerException
To avoid NullPointerException
- Make sure that all objects were initialized before using them.
- Make sure that every reference variable (object, array etc.) is nut null, before accessing the fields and methods (if any) on it.
Handling NullPointerException
Yes, you can handle NullPointerException in the main method and display your own message. And, if you haven’t handled it the program terminates abnormally throwing NPE at the run time.
Example
public class Demo { public static void main(String args[]) { int myArray[] = null; try { System.out.println(myArray[5]); }catch(NullPointerException e){ System.out.println("NPE occurred"); } } }
Output
NPE occurred
But, Since NullPointerException is a Runtime/unchecked exception there is no need to handle it at run time.
Moreover NPE occurs whenever there is a bug in the program which should be fixed, it is recommended to fix the bug or avoid it instead of trying to catch the exception.
- Related Articles
- What happens if an exception is not handled in a java program?
- What is overloading? What happens if we overload a main method in java?
- Why is NullPointerException in Java?
- What happens if the egg is not Fertilised ?
- What happens if we does not initialize variables of an interface in java?
- Is main method compulsory in Java?
- What happens if the subclass does not override abstract methods in java?
- What happens if we define a concrete method in an interface in java?
- How IllegalArgumentException automatically handled inside 'if' condition in java?
- What happens if a class does not implement all the abstract methods of an interface in java?
- Why the main () method in Java is always static?
- What happens when garbage is not disposed of properly?
- What happens if the specified index is not present in the series Python Pandas?
- How to resolve a NullPointerException in Java?
- Can we overload Java main method?
