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

2013/6/4

null pointer exception

Solringolt Solringolt

2013/6/4

#
I have a little problem, I have an object flying around my hero and when my hero has 0 life left it is removed. The flying object should also be removed (I have a condition if (lifeLeft == 0) getWorld()removeObject(this); that was working until today. Now I have a null pointer exception in my flying object class but with only one more act the program runs again. Is this problem occurring because the methods aren't called in the right order?
davmac davmac

2013/6/4

#
A NullPointerException happens because you are trying to call a method (or access a variable) via a null reference - i.e. a variable that hasn't been set to anything, or which has been set to null. Perhaps you are calling getWorld() in the constructor and then using the result. getWorld() always returns null from an actor's constructor, because the actor isn't in a world when it is being constructed.
Solringolt Solringolt

2013/6/4

#
Ok it is a bit strange but I solved my problem. Before I had this code:
public void act() 
    {
        LivesCounter livesCounter = (LivesCounter)getWorld().getObjects(LivesCounter.class).get(0);
        if (livesCounter.getValue() <= 0)     getWorld().removeObject(this); 
     if(!WorldOption.menuDisplayed)shootAlways();
        turnAround();
        
 
        
    }
    public void turnAround()   
    {  
        PlayUniverse playUniverse = (PlayUniverse)getWorld();    
        Hero player = playUniverse.getplayer();
        
        centerX = player.getX();
        centerY = player.getY();
        
        distance = 100;
        
        setLocation(centerX, centerY);  
        setRotation(angle+3);  
        angle = getRotation();  
        move(distance); 
        setRotation(0);
        
        Enemy enemy = (Enemy)getOneIntersectingObject(Enemy.class);
        if (enemy != null && !alreadyTouched && !WorldOption.menuDisplayed)
        {
            enemy.hit(1);
            alreadyTouched = true;
        }
        if (enemy == null && alreadyTouched)
        {
            alreadyTouched = false;
        }
    } 
    public void shootAlways()
    {
        if (reloadTime == 0)
        {
            getWorld().addObject(new HeroBasicShot(0,"heroBasicShot1.png"),getX(),getY());
            
            if(WorldOption.soundEffects)Greenfoot.playSound("HeroShot.mp3");
            reloadTime = 25;
                
        }
        reloadTime--;    
    }
I changed the act method to that:
public void act() 
    {
        LivesCounter livesCounter = (LivesCounter)getWorld().getObjects(LivesCounter.class).get(0);
        if (livesCounter.getValue() <= 0)
        {
            getWorld().removeObject(this);
            return;
        }
        
        if(!WorldOption.menuDisplayed)shootAlways();
        turnAround();
        
 
        
    }
Why wasn't it working before and why is it working with the new code?
davmac davmac

2013/6/4

#
In the old code, on line 4 of the act method, you remove the object from the world. Then on line 6 it calls turnAround. In turnAround (line 13) it calls getWorld() which returns null because the actor isn't in a world anymore, and on line 14 it calls a method on the null reference. (By the way, it would be much easier to figure this out if you told us which line the exception was happening on!) In the new code you return after removing the object from the world, so there is no problem.
Solringolt Solringolt

2013/6/4

#
Thanks! I thaught that when an object was removed the rest of the method wasn't called!
You need to login to post a reply.