What is a NullPointerException, and how do I fix it?

Ask a question+

What are Null Pointer Exceptions (java.lang.NullPointerException), and what causes them?

What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?

add comment

1 Answer


NullPointerException in Java is an exception class. It happens when we try to access methods and fields of an object without constructing the object using the new keyword.
So, always we need to check the object is null or not like,

if(obj != null){
//logic here

To prevent the program to terminate permanently, we need to use try-catch-finally concepts. Always put the erroneous code within a try block.

Abikananda 50

Thanks for the answer.

David Ray
add comment

Your Answer