- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to resolve a NullPointerException in Java?
A NullPointerException is a runtime exception thrown by the JVM when our application code, other 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.
Steps to resolve NullPointerException:
- 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 at the application code then a code walk-through will be required. If the problem is found from third-party API or middleware, 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 problem 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
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; npe.setField2("field2 Value"); } catch (Throwable e) { System.out.println("Java Error is: "+e ); e.printStackTrace(); } } }
Output
Java Error is: java.lang.NullPointerException java.lang.NullPointerException at NPEDemo.main(NPEDemo.java:24)
- Related Articles
- Why is NullPointerException in Java?
- When does a NullPointerException get thrown in java?
- When does a Java Array Throws a NullPointerException?
- How to resolve "Expected BEGIN_OBJECT but was BEGIN_ARRAY" using Gson in Java?
- Why can I throw null in Java and why does it upcast to a NullPointerException?
- How to resolve "Could not find or load main class package" in Java?
- What happens if NullPointerException is not handled in main method of java?
- How to resolve javac is not recognized as an internal or external command in java?
- How to Resolve DNS address using PowerShell?
- Resolve Unknown database in JDBC error with Java-MySQL?
- Does Java support multiple inheritance? Why? How can we resolve this?
- How to resolve CORS issue in C# ASP.NET WebAPI?
- Error 1046 No database Selected, how to resolve?
- How to Resolve Stale Element Reference Exception in Selenium WebDriver?
- How to resolve exception Element Not Interactable Exception in Selenium?

Advertisements