
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Resolve NullPointerException in Java
The NullPointerException is a subclass of the RuntimeException class. It is defined in java.lang package of Java. In this article, we are going to understand the reasons for NullPointerException and how to resolve them.
Reason for NullPointerException in Java
A NullPointerException is a runtime exception thrown by the JVM when our application code, another referenced API, or the middleware encounters the following conditions:
-
Attempting to invoke an instance method of a null object.
-
Attempting to access or modify a particular field of a null object.
-
Attempting to obtain the length of a null object as an array.
-
Trying to throw null instead of an actual Exception object.
Example: Throwing NullPointerException
In Java, null is a literal that does not point to any object in memory. In the below example, null does not point to any Throwable instance, due to which a NullPointerException will be thrown.
public class Example { public static void main(String[] args) { try { throwMethod(); } catch (Throwable e) { e.printStackTrace(); } } static void throwMethod() throws Throwable { throw null; } }
Output
The above code will throw a NullPointerException:
java.lang.NullPointerException: Cannot throw exception because "null" is null at Example.throwMethod(Example.java:11) at Example.main(Example.java:4)
Steps to resolve NullPointerException
You can follow the given steps to resolve and handle the NullPointerException in Java:
-
Review the java.lang.NullPointerException stack trace and determine where the Exception is triggered (Application code, third-party API, middleware software, and extract the line).
-
If the problem is in the application code, then a code walk-through will be required. If the problem is found from a third-party API or middleware, we need to first review the referenced code and determine if it could indirectly be the source of the problem, for instance, passing a null value to a third-party API method, etc.
-
If a problem is found within the application code, then attempt to determine which Object instance is null and causing the problem. We need to modify the code in order to add proper null check validations and proper logging so we can understand the source of the null value as well.
Example: Handling NullPointerException
In the following Java program, we check if an object is null or not before accessing it. This way we can avoid a NullPointerException.
public class NPEDemo { private String field1 = null; private String field2 = null; public String getField1() { return field1; } private void setField1(String field1) { this.field1 = field1; } public String getField2() { return field2; } private void setField2(String field2) { this.field2 = field2; } public static void main(String[] args) { try { NPEDemo npe = new NPEDemo(); npe.setField1("field1 value"); npe = null; // Checking null before accessing if (npe != null) { npe.setField2("field2 Value"); } else { System.out.println("Object is null, cannot set field2!"); } } catch (Throwable e) { System.out.println("Java Error is: " + e); e.printStackTrace(); } } }
Output
The above Java code prints the following output:
Object is null, cannot set field2!