Skip to main content

NullPointerException from reference passed by 'this'.

3 replies [Last post]
Joined: 2007-11-30


I have an object that receive a callback reference after creation. The callback is set like this:


Tracing the problem I found that 'this' is null when the setCallback method is invoked. Besides the obvious issue of my program not working, I am more concerned with how this has sent shockwaves through my fragile understanding of Java. Namely, how in any way can 'this' be null? Wouldn't that be impossible? I feel like I have discovered a chicken that was not hatched from an egg.

Forgive me for not posting more code, my project is quite large and I'm much more interested in hearing how 'this' can ever be null than knowing what line was at fault.

Thanks for you time,

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-04-20

Your error might have already a solution at this site:

Joined: 2007-11-30

More information...

It may be possible that my IDE telling me that 'this' == null is a result of the callback being implemented through an Interface and the IDE's inability to display the 'value' of the underlying object. I was just able to evaluate an expression through the callback reference, even thought the IDE still says the value of that object is null. I also noticed that the stack trace passes through the Interface to underlying object methods, so at this point I think my IDE is misleading me.

For posterity sake it wouldn't hurt for someone to chime in and say: "Hey Bonehead, 'this' can never be null." If that truly is the case, which I am almost certain.

Joined: 2006-12-18

A slightly kinder answer. ;)

Java enforces that "this" can not be accessed before the object exists at some scale. The fact that you are executing code that can access "this" verifies that "this" is not null.

Or so I believe,