简体   繁体   English

java Null Pointer Exception传递对象

[英]java Null Pointer Exception passing object

I have some class which creates a Ship (class Ship extends GameObject) and attempts to add it to a gameBoard. 我有一个创建Ship的类(Ship扩展GameObject类)并尝试将其添加到gameBoard中。
To do this, it tells the gameFrame to add the object as follows: 为此,它告诉gameFrame添加对象,如下所示:

public void startNewGame() {
    Ship myShip = new Ship(GAME_BOARD_WIDTH / 2, GAME_BOARD_HEIGHT-1, SHIP_WIDTH,
            SHIP_HEIGHT);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            gameFrame = new InvadersGameFrame();
        }
    });
    gameFrame.addGameObject(myShip); //Problem line
    gameFrame.repaint();
}

The gameFrame then calls: 然后,gameFrame调用:

GameBoard gameBoard = new GameBoard();
...
...
public void addGameObject(GameObject ob) {
    gameBoard.addGameObject(ob);
}

Which in turn calls: 依次调用:

public class GameBoard extends JPanel implements GameData{
    private JPanel gameBoard;
    private List<GameObject> objects = new ArrayList<>();

    public GameBoard() {
        gameBoard = new JPanel();
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        setBackground(Color.black);
        g.setColor(Color.RED);
        for(GameObject ob : objects){
            g.drawOval(ob.x, ob.y, ob.width, ob.height);
        }
    }

    //Places object into list for drawing upon next repaint.
    public void addGameObject(GameObject ob) {
        objects.add(ob);
    }
}

Now my problem is that I receive a Null Pointer Exception when I gameFrame.addGameObject(myShip); 现在我的问题是当我gameFrame.addGameObject(myShip);时,我收到一个空指针异常gameFrame.addGameObject(myShip);
The tricky thing is when I run via debugger I do not receive the NPE at all (but my objects list still seems empty). 棘手的事情是,当我通过调试器运行时,我根本没有收到NPE(但是我的对象列表似乎仍然是空的)。
Also, I can follow into each of these and still see myShip, so am I just referencing my GameObject (Ship) wrong? 另外,我可以关注其中的每一个并仍然看到myShip,所以我引用的GameObject(Ship)是否错误?
Should my parameters for addGameObject somehow be more abstract? 我的addGameObject参数应该以某种方式更抽象吗?

The problem is that you are assigning gameFrame inside of the Runnable , which does not get run until later. 问题是您要在Runnable内部分配gameFrame ,直到稍后才运行。 So gameFrame may be null at the point you call gameFrame.addGameObject(myShip) 因此,在您调用gameFrame.addGameObject(myShip) gameFrame可能为null

The problem probably is the invokeLater you use, that is scheduled whenever Swing feels like it and probably happens after gameFrame.addGameObject(myShip); 问题可能出在您使用的invokeLater上,它是在Swing感到满意时安排的,并且可能 gameFrame.addGameObject(myShip) 之后发生;

This doesnt happen in the debugger because it is somewhat of a race condition, in the debugger Swing invokes the runmethod before gameFrame.addGameObject(myShip) because you can't click fast enough to preemt that statement ;) 在调试器中不会发生这种情况,因为它有些竞争,在调试器中,Swing在gameFrame.addGameObject(myShip)之前调用运行方法,因为您不能足够快地单击以预表达该语句;)

The best solution to this problem is to move those statements inside the run method, like this: 解决此问题的最佳方法是将这些语句移动到run方法中,如下所示:

public void startNewGame() {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Ship myShip = new Ship(GAME_BOARD_WIDTH / 2, GAME_BOARD_HEIGHT-1, 
                SHIP_WIDTH, SHIP_HEIGHT);
            gameFrame = new InvadersGameFrame();
            gameFrame.addGameObject(myShip); //Problem line
            gameFrame.repaint();
        }
    });
}

Doing it this way, the 4 statements will be run in the expected order, and the attribute will not be null when required. 这样做,这4条语句将按预期顺序运行,并且在需要时该属性不会为null

check the scope where you initialized that variable. 检查初始化该变量的范围。 ie the curly braces { } around the new InvadersGameFrame(). 即在新的InvadersGameFrame()周围的花括号{}。 Make sure you are using the object after creating it 确保创建对象后使用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM