Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why null pointer exception does not provide the expression that is null?

Unless, there is a code line like, throw new NullPointerException(); null pointers usually occur when an expression that turns out to be null is operated upon. So, my question is, why does the message for the null pointer not contain the expression which is returning null?

like image 892
MozenRath Avatar asked Mar 12 '13 07:03

MozenRath


People also ask

Why does NullPointerException happen?

The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.

How do I fix NullPointerException?

In Java, the java. lang. 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.

Can NullPointerException be caught by exception?

It is generally a bad practice to catch NullPointerException. Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.

What will happen if during the test a NullPointerException will be thrown?

The NullPointerException (NPE) typically occurs when you declare a variable but did not create an object and assign it to the variable before trying to use the contents of the variable. So you have a reference to something that does not actually exist.


1 Answers

I don't know for sure, but I imagine that the reason is that there could be major performance issues if the JVM had to be able to report the precise expression that threw the exception.

At the very least, it would require changes to the class file format to include (really) fine grained source code position information ... finer grained than line numbers.

Anyway, the question is moot ... 'cos it doesn't.

like image 57
Stephen C Avatar answered Oct 12 '22 00:10

Stephen C