Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the operation attempted in case of NPE

Tags:

java

jvm

Consider the below code:

String s = null;
s.toLowerCase();

It throws a NPE:

Exception in thread "main" java.lang.NullPointerException
at pracJava1.Prac.main(Prac.java:7)

The question is: Why can't the JVM also put a helper message saying:

Exception in thread "main" java.lang.NullPointerException. Attempted toLowerCase() on null

This is useful in cases like obj.setName(s.toLowerCase()), where the line number is not sufficient to guess if obj was null or s.


On the feasibility of it, lets look at the byte code generated:

      stack=1, locals=2, args_size=1
     0: aconst_null
     1: astore_1
     2: aload_1
     3: invokevirtual #2    // Method java/lang/String.toLowerCase:()Ljava/lang/String;

So may be it does know, the method name it attempted the operation on. JVM experts, what's your opinion?

like image 667
Jatin Avatar asked Jun 28 '17 11:06

Jatin


People also ask

What is a NPE error?

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.

Can NPE be extended Java?

Java NullPointerException (NPE) is an unchecked exception and extends RuntimeException .

Can we catch NPE?

As stated already within another answer it is not recommended to catch a NullPointerException. However you definitely could catch it, like the following example shows. Although a NPE can be caught you definitely shouldn't do that but fix the initial issue, which is the Check_Circular method.

Can we handle NullPointerException in Java?

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.

Is it safe to call string equivalent in NPE?

Call String.equals (String) on ‘Safe’ Non-Null String In stead of writing below code for string comparison write above code like this. This will not cause in NPE even if param is passed as null. 4. Available NullPointerException safe operations The instanceof operator is NPE safe. So, instanceof null always returns false.

How do I avoid NPE in Java?

Avoid returning null from your methods An awesome tip to avoid NPE is to return empty strings or empty collections rather than a null. Do this consistently across your application. You will note that a bucket load of null checks become unneeded if you do so.

Why is the attempted operation prohibited in Salesforce?

The attempted operation is prohibited because it exceeds the list view threshold. The attempted operation is prohibited because it exceeds the list view threshold. 12-16-2021 09:32 PM

How to effectively handle NullPointerException in Java?

Java NullPointerException – How to effectively handle null pointer in Java. 3.1. Ternary Operator. This operator results to the value on the left hand side if not null else right hand side is evaluated. It has syntax like : 3.2. Use apache commons StringUtils for String operations. 3.3. Check Method ...


1 Answers

Why can't the JVM also put a helper message

First of all, JVM can do this. For example, SAP JVM indeed provides extended messages for NullPointerExceptions, ClassCastExceptions etc.

There were requests to do the same for HotSpot JVM, but they were closed as Will-Not-Fix for the reasons mentioned in @StephenC answer.

However, it is possible to enrich NullPointerException messages with the help of JVM TI agent.

The example of extending NPE messages

like image 134
apangin Avatar answered Nov 07 '22 03:11

apangin