This was the question asked in an interview. NullPointerException
is very common; why is it not declared as a checked exception? I googled but did not get a proper answer.
Answer: NullPointerException is not a checked exception. It is a descendant of RuntimeException and is unchecked.
NullPointerException is a runtime exception in Java that occurs when a variable is accessed which is not pointing to any object and refers to nothing or null. Since the NullPointerException is a runtime exception, it doesn't need to be caught and handled explicitly in application code.
NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.
NullPointerException is a RuntimeException . Runtime exceptions are critical and cannot be caught at compile time.
Almost every method would have to declare throwing it.
public void myMethod(String param) throws NullPointerException { // }
(As a sidenote - Eclipse for example gives you a warning whenever there is a "potential null pointer access" so that you can prevent the exception as early as possible.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With