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

2012/11/25

java.lang.NullPointerExeption

Nehit Nehit

2012/11/25

#
Hello, I keep getting this java.lang.NullPointerException error everytime I am trying to move my hero in a portal to switch the worlds. I really have no clue what to do. Help would be appreciated. Thats the error:
java.lang.NullPointerException
	at Overworld.getWorld(Overworld.java:24)
	at Portal.switchWorld(Portal.java:39)
	at Portal.act(Portal.java:25)
Line 24 in Overworld is:
        for (World world : worlds) {
Line 39 in Portal is:
World newWorld = Overworld.getWorld(worldClass);
And Line 25 in Portal is:
            switchWorld(hero);
My Code:
public class Overworld extends World
{
    public static ArrayList<World> worlds;
    
    public Overworld()
    {
        super(600, 400, 1);
        worlds = new ArrayList<World>();
        worlds.add(new Level1());
        worlds.add(new Level2());
        Greenfoot.setWorld(worlds.get(0));
    }
    public static World getWorld(Class worldClass)
    {
        for (World world : worlds) {
            if (world.getClass() == worldClass) {
                return world;
            }
        }
        return null;
    }
}

public class Portal extends Actor
{
    Class worldClass;
    int x;
    int y;
    
    public Portal(Class worldClass, int x, int y)
    {
        this.worldClass = worldClass;
        this.x = x;
        this.y = y;
    }
    public void act() 
    {
        Hero hero = (Hero) getOneIntersectingObject(Hero.class);
        if (hero != null) {
            switchWorld(hero);
        }
    }
    private void switchWorld(Hero hero)
    {
        int invX;
        int invY;
        invX = hero.getInventory().getX();
        invY = hero.getInventory().getY();
        
        World oldWorld = getWorld();
        oldWorld.removeObject(hero.getInventory());
        oldWorld.removeObject(hero);
        
        World newWorld = Overworld.getWorld(worldClass);
        newWorld.addObject(hero.getInventory(), invX, invY);
        newWorld.addObject(hero, x, y);
        
        Greenfoot.setWorld(newWorld);
    }
}
Gevater_Tod4711 Gevater_Tod4711

2012/11/25

#
Probably the failur is in line 55 because newWorld is null. If your method getWorld(Class worldClass) returns null there is an exception. Another answer could be that in line 16 world.getClass() is the line that throws this NullPointerException because world is null. (I'm not sure if an ArrayList can be null but if it is null that would be the answer.)
danpost danpost

2012/11/25

#
It really would not matter if an ArrayList can be null or not; the method where line 16 is will return null of none of the worlds classes match the parameter variable due to the programming. I think line 16 should be tested to ensure that when a match should arise, that it is triggered by that statement. You may have to use the 'equals' method to compare the classes. Also, after the method is called (because it may return null), there should be a check for a null value.
You need to login to post a reply.