AWT-EventQueue-1 NullExceptionPointer

时间:2014-08-30 06:43:54

标签: java applet synchronized

我正在使用瓷砖进行迷宫游戏。迷宫定期改变形状。 此时,在重建迷宫之前,tilearray变空。 我在不同的时间间隔之后得到NPE错误,并在一段时间后游戏停止。 可能是因为某些方法在实际重新创建之前尝试访问。 我试图在某些地方放置if(...!= null),甚至试图同步一些部分(我并不熟悉)。

编辑:感谢Arvind指出问题所在。我设法解决了这个问题,但我仍然会更多地帮助它,因为它不是我认为最好,最整洁的解决方案。 在某些情况下,Tile t等于null。我只是检查了它并继续; 进入for循环以避免错误。

非常感谢您提前提供任何帮助和建议!

有了这个错误,游戏仍在运行:

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at shiftingmaze.Game.paintTiles(Game.java:161)
at shiftingmaze.Game.paint(Game.java:115)
at shiftingmaze.Game.update(Game.java:107)
at sun.awt.RepaintArea.updateComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

过了一会儿它冻结了这个错误:

Exception in thread "Thread-4" java.lang.NullPointerException
at shiftingmaze.Game.updateTiles(Game.java:153)
at shiftingmaze.Game.run(Game.java:86)
at java.lang.Thread.run(Unknown Source)

以下是代码的一些部分:

@Override
public void start() {

    hero = new Hero();

    Timer t = new Timer();

    t.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            tilearray.clear();
            shiftMaze();
        }
    }, 0, 1000);

    Thread thread = new Thread(this);
    thread.start();
}

@Override
public void stop() {

}

@Override
public void destroy() {

}

@Override
public void run() {

    while (true) {
        hero.update();
        updateTiles();
        repaint();
        try {
            Thread.sleep(17);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void update(Graphics g) {

    if (image == null) {
        image = createImage(this.getWidth(), this.getHeight());
        second = image.getGraphics();
    }

    second.setColor(getBackground());
    second.fillRect(0, 0, getWidth(), getHeight());
    second.setColor(getForeground());
    paint(second);
    g.drawImage(image, 0, 0, this);
}

@Override
public void paint(Graphics g) {

    g.drawImage(background, 0, 0, this);
    paintTiles(g);
    // g.drawRect((int)Hero.rect.getX(), (int)Hero.rect.getY(),
    // (int)Hero.rect.getWidth(), (int)Hero.rect.getHeight());
    // g.drawRect((int)Hero.bigRect.getX(), (int)Hero.bigRect.getY(),
    // (int)Hero.bigRect.getWidth(), (int)Hero.bigRect.getHeight());
    g.drawImage(character, hero.getHeroX(), hero.getHeroY(), this);
}

public void shiftMaze() {

    final int MAZEROW = 24;
    final int MAZECOL = 40;

    for (int i = 0; i < MAZEROW * MAZECOL / 2; i++) {
        int n = rand.nextInt(MAZECOL - 2) + 1;
        int m = rand.nextInt(MAZEROW - 2) + 1;
        if (!(n == 1 && m == 1) && !(n == MAZECOL - 2 && m == MAZEROW - 2)) {
            Tile t = new Tile(n, m);
            tilearray.add(t);
        }
    }

    for (int i = 0; i < MAZECOL; i++) {
        for (int j = 0; j < MAZEROW; j++) {
            if (i == 0 || i == MAZECOL - 1 || j == 0 || j == MAZEROW - 1)
                if (!(i == 1 && j == 0)
                        && !(i == MAZECOL - 2 && j == MAZEROW - 1)) {
                    Tile t = new Tile(i, j);
                    tilearray.add(t);
                }
        }
    }
}

public void updateTiles() {

    for (int i = 0; i < tilearray.size(); i++) {
        Tile t = (Tile) tilearray.get(i);
        t.update();
    }
}

public void paintTiles(Graphics g) {

    for (int i = 0; i < tilearray.size(); i++) {
        Tile t = (Tile) tilearray.get(i);
        g.drawImage(t.getTileImage(), t.getTileX(), t.getTileY(), this);
    }
}

这是来自Tile类:

public Tile(int x, int y) {

    tileX = x * 20;
    tileY = y * 20;
    tileImage = Game.getWall();

    r = new Rectangle();
}

public void update() {

    r.setBounds(tileX, tileY, 20, 20);
    if (r.intersects(Hero.bigRect))
        checkCollision(Hero.rect);
}

2 个答案:

答案 0 :(得分:0)

问题在于:

public void updateTiles() {
    for (int i = 0; i < tilearray.size(); i++) {
        Tile t = (Tile) tilearray.get(i);//<-- tile is null in some cases
        t.update();
    }
}

有很多选择可以避免这种情况:

选项1 :子类ArrayList并使用null检查覆盖方法add()和addAll()。

选项2 :填充tilearray后,使用此行删除空值

    tilearray.removeAll(Collections.singleton(null));

<强> 修改

如果您正在寻找第二种选择,那么您应该尝试这样做:

public void shiftMaze() {
    //<-- to do here
    tilearray.removeAll(Collections.singleton(null));//<-- at the end
}

答案 1 :(得分:0)

感谢您的帮助,我试过了一个,但它仍然得到一些空值。 与此同时,我做了以下以摆脱错误。它一直工作ALMOST但很少我仍然得到NPE或Index越界错误... 我把游戏加速到100毫秒娱乐,以便长期进行错误检查。

for (int i = 0; i < tilearray.size(); i++) {
    Tile t = (Tile) tilearray.get(i);
    if (t == null && i < tilearray.size() - 1) {
        System.out.println("t = null @ updateTiles(), moving to next element");
        continue;
    }
    if (t == null && i >= tilearray.size()) {
        System.out.println("t = null @ updateTiles(), no elements left");
        break;
    }
    t.update();