I had a serious bug in my code, which luckily I found :), because I forgot to change the method's return type signature (from int to Integer) and my app was crashing without any warning.
I had the following method:
public static int getDataSourceIdForName(String name) {
Integer i = dsNameToIdMap.get(name);
return i;
}
It was crashing my app. I observed that it was crashing only when a special case occurred and the name
was actually not in the map ( Map<String, Integer> dsNameToIdMap = new HashMap<>()
) thus it was returning null. I would expect a running application to throw a NullPointerException
but this was not the case and the app was simply dying and I had a trip full of excitement to find the bug :)
Since I would like to have my IDE's support in avoiding those situations in future, I am asking you guys how to set NetBeans
to warn of those situations, ie when I am returning an object in a function which return signature is a primitive type, as I cannot figure it out myself.
UPDATE: I have found the real problem. The code is in fact throwing the NullPointerException
, as Jon rightly argued :) It was only my clients 'survival mechanism' that was hiding it, as you can see in the sample below:
public class WhyNoNullPointerException {
private static int test() {
Integer i = null;
return i;
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("i = " + i);
if (i == 10) {
try{
test();
}finally{
run();
}
}
}
}
});
t.setDaemon(true);
t.start();
Thread.sleep(1111);
}
}
Since the program in finally tries to stay alive by restarting its main operational loop, for this reason it 'hides' the NullPointerException
.
To avoid this situation in future I will now always, in this self restarting code, add a catch for Exception before the finally block as a good practice this way an exception will not be 'lost' (I had the block in the first place in my code I had a byte reading there which can throw IOException
).
As to development of a hint I found a very nice tutorial over which I will go today and hopefully will have a working hint (link below). Thanks for your links guys (@JonSkeet and @Nambari) they were very useful in finding some more about the hint development.
It really does throw NullPointerException
:
import java.util.*;
public class Test {
private static final Map<String, Integer> map
= new HashMap<String, Integer>();
public static void main(String[] args) {
getInt("Foo");
}
public static int getInt(String name) {
Integer i = map.get(name);
return i;
}
}
Result:
c:\Users\Jon\Test>java Test
Exception in thread "main" java.lang.NullPointerException
at Test.getInt(Test.java:13)
at Test.main(Test.java:8)
Now you may or may not also want NetBeans to warn on auto-unboxing... but it's worth at least checking what's going on first :)
I found a plugin which highlights auto-boxing/unboxing and varargs, but it's very old and may well not be supported on the version you're using.
I'd expect this to be configurable as a compiler option (it is in Eclipse) but I don't have enough experience with NetBeans to say much more.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.