DEVFYI - Developer Resource - FYI

What is NullPointerException and how to handle it?

Java Interview Questions and Answers (part 1)


(Continued from previous question...)

5. What is NullPointerException and how to handle it?

When an object is not initialized, the default value is null. When the following things happen, the NullPointerException is thrown:
--Calling the instance method of a null object.
--Accessing or modifying the field of a null object.
--Taking the length of a null as if it were an array.
--Accessing or modifying the slots of null as if it were an array.
--Throwing null as if it were a Throwable value.
The NullPointerException is a runtime exception. The best practice is to catch such exception even if it is not required by language design.

(Continued on next question...)

Other Interview Questions