views:

87

answers:

3

in a simplified version, what i have is this:

public class MyLabel extends JLabel implements MouseListener{
private SomeControl control;

public MyLabel(SomeControl control){
 this.addMouseListener(this);
 this.control = control;
}

@Override
public void mouseClicked(MouseEvent arg0) {
 Object x = this.control.getSomeProperty();
}

Even though i debug and verify when constructing the MyLabel instance that the control and its someProperty is not null, when the event is fired and handler steps in, it shows the someProperty as if it was null, what could be the problem here?

A: 

I'm pretty sure the property is set to null somewhere else in your code. You can try setting a watchpoint on the someProperty field and debug to see when it becomes null.

Or you could provide us with some compilable code, so we could try it ourselves.

Fortega
it is pretty complex code, it would be even more difficult for someone else to test it, i don't assign null anywhere in the code, it might be the case that i don't assign anything in the first place but why then i can see it taking "control" and its "someProperty" properly?
mustafabattal
i mean in the constructor, it seems all the properties are fine
mustafabattal
+1  A: 

if you can do it change the code to:

(EDIT: too early in the morning.. .forgot the word final :-)

private final SomeControl control;

You will have to do the "control = new ......" in the constructor, but if you only want it to be assigned in one place then that is the way to get the compiler to help you.

Also, are you sure that the NullPointerException is not in the method call?

it shows the someProperty as if it was null, what could be the problem here?

Based on that I am assuming I misunderstood what you were saying... are you saying that the someProperty() method call returns null? If that is the case, then do the following:

  • in the SomeControl class mark the someProoerty variable as final (if possible). That means that you cannot have a setProperty(....) method.
  • if you cannot make the variable final add the following code to the setProperty(...) method:

...

public void setPropert(... value)
{
    if(value == null)
    {
        throw new IllegalArgumentException("value cannot be null");
    }
}

then you will see what part of the code is setting the property to null.

TofuBeer
i want those objects not to lose their connection between, if i use new, that would be completely different object from the one in parameter. does that mean i already lose that connection with this type of assignment?
mustafabattal
if you only want it to be set once then using the word "final" lets the compiler do that. If you really do want to change the control then marking it as final won't work for you.
TofuBeer
well, debugger shows the property as null, so i think it's not thrown from inside method, however this gets more and more complicated, i will have to find another workaround for this, thank you so much for the help
mustafabattal
+1  A: 

In the MyLabel constructor, the value of control is null (by default) when this is added as the MouseListener. If the listener were invoked at this point, it would see the null value. Subsequently, the control value is updated with the non-null parameter value, as the debugger reports. I suspect the anomaly may be an escaped this, as discussed here.

trashgod