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

2011/12/8

Null Pointer exception

darkmist255 darkmist255

2011/12/8

#
Now, this is caused by a reference, but I've looked it over again and again and can't figure out what's wrong.
public class Spikes extends Actor
{
    Room room = (Room)getWorld();
    Stats stats = room.stats;
    Character character = room.character;
    
    private int delay = 0;
    
    public void act() 
    {
        if(delay > 0)
        {
            delay = (delay - 1);
        }
        if(getOneIntersectingObject(Character.class) != null && delay == 0 && (character.getY() - 5) >= (getY() - 25))
        {
            //make it damage player on collision
            stats.damagePlayer(1);
            
            delay = 20;
        }
    }    
}
All of the above works, except it says there is an error at the line "Character character = room.character;" In the room class there is
    public Character character = new Character();

.......

    addObject(character, 425, 525);
I tried making it public static Character character = new Character(); but then the world just didn't load at all. Derp.
AwesomeNameGuy AwesomeNameGuy

2011/12/8

#
Well, looks like character points to a character object, and the character in spikes should then point to the same object, so personally, I don't know about this one! And you are sure that's where the error is? Why would that cause a null pointer exception I wonder...is there other code in room that dereferences the variable character? Now personally, I use methods to get objects, like in room I would have a method called getCharacter() that returns the character something like this: character = room.getCharacter(); it's just standard practice, but I wonder what could be causing this error myself.
darkmist255 darkmist255

2011/12/8

#
I'll try a method (that's what I used to do, maybe it's less buggy), but yes I'm pretty sure that's what's causing the error, since without it there's no error. **update** Nope, there's still a Null Pointer with a method.
kiarocks kiarocks

2011/12/8

#
I see your problem. When you initially get the world, it returns null. You need to keep geting the world in your act() method first thing.
public class Spikes extends Actor  
{  
    Room room = (Room)getWorld();  
    Stats stats = room.stats;  
    Character character = room.character;  
      
    private int delay = 0;  
      
    public void act()   
    {  
        room = (Room)getWorld(); //see the new line?
        if(delay > 0)  
        {  
            delay = (delay - 1);  
        }  
        if(getOneIntersectingObject(Character.class) != null && delay == 0 && (character.getY() - 5) >= (getY() - 25))  
        {  
            //make it damage player on collision  
            stats.damagePlayer(1);  
              
            delay = 20;  
        }  
    }      
}  
darkmist255 darkmist255

2011/12/8

#
Ahh, I see. Strange how I've never got this error before, but thanks! New code:
public class PlainRock extends Actor
{
    Room room = (Room)getWorld();
    Stats stats = room.stats;
    //Character character = room.character;
    
    public void act() 
    {
        Room room = (Room)getWorld();
        Character character = room.character;
        if(getOneIntersectingObject(Character.class) != null)
        {
            //make it damage player on collision (for a health test)
            stats.damagePlayer(1);
            
        }
        while((character.getX() + 25) >= (getX() - 25) && (character.getX() + 25) <= getX())
        {
            character.setLocation((getX() - 1), getY());
        }
    }    
}
kiarocks kiarocks

2011/12/8

#
glad to help
AwesomeNameGuy AwesomeNameGuy

2011/12/8

#
Ahh that's right. Forgot about that.
davmac davmac

2011/12/8

#
Another solution is to use the "addedToWorld" method to find the world. This avoids having to "getWorld()" every time:
    public class PlainRock extends Actor  
    {  
        Room room;  
        Stats stats;  
        Character character;  
          
        public void act()   
        {  
            if(getOneIntersectingObject(Character.class) != null)  
            {  
                //make it damage player on collision (for a health test)  
                stats.damagePlayer(1);  
                  
            }  
            while((character.getX() + 25) >= (getX() - 25) && (character.getX() + 25) <= getX())  
            {  
                character.setLocation((getX() - 1), getY());  
            }  
        }      
    
        protected void addedToWorld(World world)
        {
            room = (Room) world;
            stats = room.stats;
            character = room.character;
        }
    }
darkmist255 darkmist255

2011/12/9

#
I've seen that protected void addedToWorld before, what exactly is it?
darkmist255 darkmist255

2011/12/9

#
And are those parameters the same each time? Or do they change depending on the context?
kiarocks kiarocks

2011/12/9

#
AddedToWorld is called when the actor is added to the world.
darkmist255 darkmist255

2011/12/9

#
Ahhhhh! I get it, thanks :D.
You need to login to post a reply.