Jun. 26th, 2014

semperfiona: (dilbert)
No 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

     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.

Profile

semperfiona: (Default)
semperfiona

September 2024

S M T W T F S
1234567
891011121314
15161718192021
2223242526 2728
2930     

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 20th, 2025 08:52 pm
Powered by Dreamwidth Studios