NullPointerException when returning new []

I have a very strange problem with a NullPointerException. Sample code is as follows:

... ... public String[] getParams(...) { ... ... ... ... 143 return new String[] { 144 getUserFullName(), 145 StringUtil.formatDate(sent), . tiltu, . StringUtil.initCap(user.getName()), . vuosi.toString(), . asiatyyppi[0] + " " + lisatiedot[0], . asiatyyppi[1] + " " + lisatiedot[1], . alaviitteet[0], 152 alaviitteet[1]}; 153 } 

Now I have a production issue with stack trace:

 java.lang.NullPointerException at package.EmailService.getParams(EmailService.java:143) ... 

I cannot make such a stack trace. This may be an environmental issue that, for some reason, does not match line numbers. If I have null references to any trace points of the variable stack on this particular line, but never on line 143.

But I want to ask: is it possible to throw a NullPointerException on line 143 specifically?

+7
java nullpointerexception
source share
2 answers

The line number in the stack trace comes from the LineNumberTable attribute in the class file. ( See JVM specification )

It would not be a problem to output the correct line number for subexpression - all the compiler should do is say that from the index of byte x to y there is a correspondence with the original code line z.

But up to Java 1.7, including a bug in the compiler that was fixed in 1.8:

https://bugs.openjdk.java.net/browse/JDK-7024096

A DESCRIPTION OF THE PROBLEM: linenumbertable only in compiled code has line numbers to run statements. When an expression uses the chaining method or another API of the "free interface" of the interface, the operator can span dozens of lines and contain hundreds of method calls.

Currently, throwing exceptions from any of these methods will have the line number of the first line of the attached statement, which is very difficult to debug a method call that has a problem.

linnumbertable must have the correct line numbers for each method call.

-

BT2: EVALUATION

It seems simple enough to fix this, but its kind of risky at the end of the jdk7 development cycle aimed at jdk8.

So, in 1.7 you get the wrong line number for these subexpressions (if they occurred in the same method, although - if you call another method in the subexpression, and the other method throws a NullPointerException, you will see it reporting it - maybe it’s so error is not always a big problem)

One way you could get around this is to build a Java 8 compiler to compile the source code and use the javac -source 1.7 -target 1.7 flags. But it would be better and safer to upgrade the prod environment to 1.8.

+5
source share

Consider the source code that defines the new String array:

 return new String[] { getUserFullName(), StringUtil.formatDate(sent), tiltu, StringUtil.initCap(user.getName()), vuosi.toString(), asiatyyppi[0] + " " + lisatiedot[0], asiatyyppi[1] + " " + lisatiedot[1], alaviitteet[0], alaviitteet[1]}; } 

If any of the elements of the built-in array should NullPointerException , the JVM will interpret the Exception as having occurred in the line where the definition started. In other words, the JVM will consider the above code as the following:

 return new String[] { getUserFullName(), StringUtil.formatDate(sent), tiltu, StringUtil.initCap user.getName()), vuosi.toString(), asiatyyppi[0] + " " + lisatiedot[0], asiatyyppi[1] + " " + lisatiedot[1], alaviitteet[0], alaviitteet[1]}; } 

where everything is on the same line.

If you really want to handle a NullPointerException here, you must define the variables outside of creation.

+5
source share

All Articles