Java: how can an impossible NullPointerException still occur?
I have just received the following error report about our software:
java.lang.NullPointerException at java.util.Arrays.equals(UnkNown Source) at our.app.OurMain(OurMain.java:13)
This happens with JRE 1.7.0 on windows 0_ 45, the corresponding source code is arrays equals:
public static boolean equals(byte[] a,byte[] a2) { if (a==a2) return true; if (a==null || a2==null) return false; int length = a.length; if (a2.length != length) return false; for (int i=0; i<length; i++) if (a[i] != a2[i]) return false; return true; }
The calling code is:
final byte[] b1 = ... // populate array final byte[] b2 = ... // populate array final boolean equal = Arrays.equals(b1,b2);
Obviously, there is no way to raise NullPointerException here How can it happen? Error reports can be considered trustworthy
Solution
This is not an answer, but because the comments are too long
The question that has not yet been raised is: when did the problem begin to appear? After a specific software update or sudden appearance? You may not be able to track the exact date when it first occurred, but if it can be associated with time, anything you provide as software may change (for example, do you bundle JRE?) Cause problems Then, you will methodically distinguish between the known pre - bug version and the first bug list release
If it suddenly appears, the most likely source of the problem (as far as I know, what you disclosed) is hidden somewhere in the software execution environment (may not be under your direct control) It may be the JRE itself, related libraries or system services, or even a specific combination of hardware and software (believe it or not, sometimes obviously even the BIOS of the workstation will have a huge impact: https://www.daniweb.com/hardware-and-software/microsoft-windows/windows-vista-and-windows-7-8/threads/271699 ).
In order to improve the chance of actually solving the cause of the problem, you need to collect the information of the environment in which the error actually occurs and try your best (at least JRE version, 32 / 64 bit, operating system; it is best to install patch and CPU model; it is best to include mask setting revision and motherboard model; it is best to include BIOS version and motherboard version) Don't forget the exact version or the software that caused the problem If your user base has a very homogeneous environment (for example, only a few workstation models have been purchased in a large company, and all may come from the same supplier), these small details are more meaningful than a very heterogeneous user base (for example, used by many independent customers) Completely different configuration)
Given that sufficient data patterns should appear (all error reports have something in common, such as a specific JRE; or a set of JRE versions, a specific workstation model, etc.)
If possible, your users can cooperate with you (deliberately changing the environment of the machine / user experiencing the problem) to test candidates This may involve installing JDK to get more error context, changing VM settings, etc
It is only a rough outline, but if there is no quick solution, in the long run, a structured approach is most likely to produce a solution