This site requires JavaScript, please enable it in your browser!
Greenfoot back
darkmist255
darkmist255 wrote ...

2012/9/7

Why is this throwing a NullPointerException?

darkmist255 darkmist255

2012/9/7

#
I don't see why this is throwing a NullPointer, can anyone fill me in? Ask me if you need any more code provided. I just can't find what it is here...
    public void followTarget()
    {
        //if(!this.equals(PersistentStorage.playerTargetSelector)) remove = true;
        if(PersistentStorage.playerTarget == null)
        {
            target = PersistentStorage.playerTarget;
        }
        if(!target.equals(PersistentStorage.playerTarget)) //THIS IS THE EXCEPTION LINE
        {
            target = PersistentStorage.playerTarget;
        }
        if(target != null)
        {
            rePosition(target.coordX, target.coordY);
            if(target.getImage().getWidth() != getImage().getWidth() || target.getImage().getHeight() != getImage().getHeight())
            {
                setImage(imageFile);
                getImage().setTransparency(alpha);
                getImage().scale(target.getImage().getWidth(), target.getImage().getHeight());
            }
        }
        else if(!getImage().equals(blankimg))
        {
            setImage(blankimg);
        }
    }
darkmist255 darkmist255

2012/9/7

#
I can already see that "target.equals" might be my null pointer... As usual, after I post it I soon after discover the reason. Not sure yet, but that's most likely the problem. I'll report back with results. EDIT -- Yup, I've added in a check for target being null, not sure why I never saw that before.
danpost danpost

2012/9/7

#
darkmist255 wrote...
I don't see why this is throwing a NullPointer, can anyone fill me in?
if(PersistentStorage.playerTarget == null)
{
     target = PersistentStorage.playerTarget;
}
if(!target.equals(PersistentStorage.playerTarget)) //THIS IS THE EXCEPTION LINE
Yeah, looking at the 'if' statement, target could be 'null' for the next!
darkmist255 darkmist255

2012/9/7

#
That's hilarious, I removed that in my code when I figured out the problem, I never even realized how backwards that was. Oh the joys of small changes causing big problems.
You need to login to post a reply.