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

2020/4/7

NullPointerException?

bubbadawg bubbadawg

2020/4/7

#
Hello, I get a nullPointerException when from line 6, where I try to add the Tile object that I just created in the 2D array. Any ideas why I am getting this? Thanks!
        
        for (int r = 0; r < tiles.length; r++)
        {
            for (int c = 0; c < tiles[r].length; c++)
            {
                tiles[r][c] = new Tile(theXCoord, theYCoord);
                getWorld().addObject(tiles[r][c], tiles[r][c].getXCoord(), tiles[r][c].getYCoord());
                theXCoord += 50;
            }
            theXCoord = 170;
            theYCoord += 50;
        }
Zamoht Zamoht

2020/4/7

#
Where do you call this from? It seems like getWorld() returns null, which probably means you are doing this, before the actor is added to the world.
bubbadawg bubbadawg

2020/4/7

#
I am calling this as part of the constructor for one of my actor classes Edit: You're right. I moved that code to the constructor of my World subclass and it worked when I removed the "getWorld()." in line 6. Thank you!
You need to login to post a reply.