I've just found a very strange NullPointerException. First, I create a NumberFormat like this (note that the default Locale would be Germany, I don't know if that helps):
NumberFormat angleFormat = NumberFormat.getNumberInstance(Locale.UK);
angleFormat.setMaximumFractionDigits(5);
angleFormat.setMinimumFractionDigits(0);
Then, I tried to format a double with it. This is done with a new Thread created by a Lambda, while angleFormat is declared at the method containing the Lambda. The code where the Exception is thrown looks like this:
con.println("D" + moveId + (state.isEnemyInSightOf(e) ? "+" : "-")
+ angleFormat.format(e.getAngle()) // line 123 - error is here
+ (state.isMissileInSightOf(e) ? "+" : "-")
+ angleFormat.format(e.getSight())
+ (e.getLastShot() >= 10 || e.getLastShot() <= -1 ? "+" : "-")
+ angleFormat.format(e.getLives()));
e.getAngle() returns a double, so it can't return null. However, I get this Exception:
Exception in thread "Thread-1" java.lang.NullPointerException
at java.text.DecimalFormat.fastDoubleFormat(Unknown Source)
at java.text.DecimalFormat.fastFormat(Unknown Source)
at java.text.NumberFormat.format(Unknown Source)
at server.game.Simulator.lambda$0(Simulator.java:123)
at server.game.Simulator$$Lambda$3/23162747.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I'm sure that e isn't null because of the Exception's stacktrace, it would a) be thrown one line earlier and b) not at java.text.DecimalFormat.fastDoubleFormat
Why is there a NullPointerException beeing thrown sometimes, and sometimes it works without problems? And what does that mean? The error seems to be reproducable, but not very often.
"NullPointerException beeing thrown sometimes, and sometimes it works without problems? And what does that mean?"-- this intermittent error smells of a threading issue. Is this a GUI? Are you careful with your code's threading?eitself is notnull?