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

2013/12/9

java.lang.NullPointerException problem

Delauras Delauras

2013/12/9

#
I keep getting this error when i try to run my game and I don;t seem to understand what is the problem. This is the error i getError And this is when i click on in: Code
Delauras Delauras

2013/12/9

#
I really don't understand why there is a problem with an if statement.
danpost danpost

2013/12/9

#
Please post your code using the 'code' tag below the 'Post a reply' box and copy/paste the error message you are getting.
Delauras Delauras

2013/12/9

#
public class GameBoard extends World
{
    GameBall cell_1, cell_2, cell_3, cell_4, cell_5, cell_6, cell_7, cell_8, cell_9;
    Player player;

    /**
     * Constructor for objects of class GameBoard.
     * 
     */
    public GameBoard()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(3, 3, 60);
        setPaintOrder(Player.class, GameBall.class);
        setBackground("cell.jpg");
        
        GameBall cell_1 = new GameBall();
        addObject (cell_1, 0, 0);
        
        GameBall cell_2 = new GameBall();
        addObject (cell_2, 1, 0);
        
        GameBall cell_3 = new GameBall();
        addObject (cell_3, 2, 0);
        
        GameBall cell_4 = new GameBall();
        addObject (cell_4, 0, 1);
        
        GameBall cell_5 = new GameBall();
        addObject (cell_5, 1, 1);
        
        GameBall cell_6 = new GameBall();
        addObject (cell_6, 2, 1);
        
        GameBall cell_7 = new GameBall();
        addObject (cell_7, 0, 2);
        
        GameBall cell_8 = new GameBall();
        addObject (cell_8, 1, 2);
        
        GameBall cell_9 = new GameBall();
        addObject (cell_9, 2, 2);
        
        player = new Player();
        addObject(player, 0, 0);
        
    
    }
    public void act()
    {
        if (
             (cell_1.isGold() && cell_2.isGold() && cell_3.isGold()) ||
             (cell_4.isGold() && cell_5.isGold() && cell_6.isGold()) ||
             (cell_7.isGold() && cell_8.isGold() && cell_9.isGold()) ||
             (cell_1.isGold() && cell_4.isGold() && cell_7.isGold()) ||
             (cell_2.isGold() && cell_5.isGold() && cell_8.isGold()) ||
             (cell_3.isGold() && cell_6.isGold() && cell_9.isGold()) ||
             (cell_1.isGold() && cell_5.isGold() && cell_9.isGold()) ||
             (cell_3.isGold() && cell_5.isGold() && cell_7.isGold())
            )
         {
            cell_1.setWinIfGold(); 
            cell_2.setWinIfGold(); 
            cell_3.setWinIfGold();
            cell_4.setWinIfGold(); 
            cell_5.setWinIfGold(); 
            cell_7.setWinIfGold(); 
            cell_8.setWinIfGold(); 
            cell_9.setWinIfGold();
            player.setPlayer2();
            Greenfoot.stop();
        }
        else
        {
            if (
                (cell_1.isSteel() && cell_2.isSteel() && cell_3.isSteel()) ||
                (cell_4.isSteel() && cell_5.isSteel() && cell_6.isSteel()) ||
                (cell_7.isSteel() && cell_8.isSteel() && cell_9.isSteel()) ||
                (cell_1.isSteel() && cell_4.isSteel() && cell_7.isSteel()) ||
                (cell_2.isSteel() && cell_5.isSteel() && cell_8.isSteel()) ||
                (cell_3.isSteel() && cell_6.isSteel() && cell_9.isSteel()) ||
                (cell_1.isSteel() && cell_5.isSteel() && cell_9.isSteel()) ||
                (cell_3.isSteel() && cell_5.isSteel() && cell_7.isSteel())
               )
            {
                cell_1.setWinIfSteel();
                cell_2.setWinIfSteel();
                cell_3.setWinIfSteel();
                cell_4.setWinIfSteel();
                cell_5.setWinIfSteel();
                cell_6.setWinIfSteel();
                cell_7.setWinIfSteel();
                cell_8.setWinIfSteel();
                cell_9.setWinIfSteel();
                player.setPlayer1();
                Greenfoot.stop();
            }
        }
    }
}
Delauras Delauras

2013/12/9

#
java.lang.NullPointerException at GameBoard.act(GameBoard.java:59) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2013/12/9

#
The problem is in the constructor (lines 17, 20, 23, 26, 29, 32, 35, 38 and 41) where you are creating local fields ('cell_1' through 'cell_9') with the same name as your instance fields (which are already declared). Remove 'GameBall' from those lines so that the instance fields will be used instead of your newly created local fields.
Delauras Delauras

2013/12/9

#
Thank you that worked fine :)
You need to login to post a reply.