(no subject)
Jun. 26th, 2014 06:22 pmNo one but another Java programmer is likely to understand this, but since one way I learn things for good is by writing them down, I'm doing it anyway.
The last three days I've been trying to figure out a NullPointerException when calling the constructor of a new class I'd made. "But there's no objects being accessed *on* that line other than the one I'm building!" I kept thinking. "Why isn't the debugger even *going* to the constructor?"
I've just finally figured it out. There was an object access on that line. A very sneaky one: I created the constructor to take an int primitive...and I was passing in an Integer object, relying on the automatic unboxing of primitive wrapper objects that has been part of Java since Java 5. And my Integer object...was null. Heh. I'd have gotten it in 1/2 second if the line throwing the NPE had read
but since it read
it went right past me. FOR DAYS.
I tried everything I could think of. I googled and read all the documents on constructors and NullPointerExceptions I could find, all of which were either oriented to beginners or specific to older versions of Java, I moved the class definition around in its containing class, and because I simply could not believe there was a problem I ran and reran the same error-filled code. BAH.
So now I know. And now you-all also know that even with over 10 years experience doing Java, I can still miss things.
The last three days I've been trying to figure out a NullPointerException when calling the constructor of a new class I'd made. "But there's no objects being accessed *on* that line other than the one I'm building!" I kept thinking. "Why isn't the debugger even *going* to the constructor?"
I've just finally figured it out. There was an object access on that line. A very sneaky one: I created the constructor to take an int primitive...and I was passing in an Integer object, relying on the automatic unboxing of primitive wrapper objects that has been part of Java since Java 5. And my Integer object...was null. Heh. I'd have gotten it in 1/2 second if the line throwing the NPE had read
MyObject obj = new MyObject(someInteger.intValue());
but since it read
MyObject obj = new MyObject(someInteger);
it went right past me. FOR DAYS.
I tried everything I could think of. I googled and read all the documents on constructors and NullPointerExceptions I could find, all of which were either oriented to beginners or specific to older versions of Java, I moved the class definition around in its containing class, and because I simply could not believe there was a problem I ran and reran the same error-filled code. BAH.
So now I know. And now you-all also know that even with over 10 years experience doing Java, I can still miss things.