Java AWT-EventQueue-1 NullExceptionPointer

Java AWT-EventQueue-1 NullExceptionPointer,java,applet,synchronized,Java,Applet,Synchronized,我正在用瓷砖做一个迷宫游戏。迷宫每隔一段时间就会改变形状。 此时,在迷宫重新创建之前,Tilerray变空。 在不同的时间间隔后,我会出现NPE错误,过了一段时间游戏就停止了。 可能是因为某些方法在实际重新创建之前尝试访问。 我试着把如果=在某些地方为null,甚至尝试同步一些我不太熟悉的部分 编辑:感谢Arvind指出问题所在。我设法修复了它,但我仍然希望得到更多的帮助,因为我认为这不是最好、最整洁的修复方法。 在某些情况下,Tile t等于null。我只是简单地检查了一下,然后继续; 进入f

我正在用瓷砖做一个迷宫游戏。迷宫每隔一段时间就会改变形状。 此时,在迷宫重新创建之前,Tilerray变空。 在不同的时间间隔后,我会出现NPE错误,过了一段时间游戏就停止了。 可能是因为某些方法在实际重新创建之前尝试访问。 我试着把如果=在某些地方为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);
    }
}
问题在于:

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();
    }
}
编辑

如果您正在寻找第二种选择,则应尝试以下方法:

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

谢谢你的帮助,我试过了,但还是有空。 同时,我做了以下几点来消除错误。它几乎一直在工作,但很少我仍然得到NPE或索引越界错误。。。 我将游戏速度提高到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();

请检查此处的Tile是否为空:Tile t=Tile tileray.geti;在方法UpdateLifesTilerRay中,TilerRay在哪里声明和初始化?也许这是空的。尝试调试您的代码。@Arvind:我选中了,是的,当它抛出AWT EventQueue时,它是空的-1@Jens:一开始,在init private ArrayList之前,tilerarray=new ArrayList@Kokufuu你把'shiftMaze'叫做哪里?我应该把它插入哪里?我试过了,但仍然收到错误。。。在shiftMaze方法之后?这是清理后的填充位置。谢谢!我仍然会出错,但至少我知道在哪里搜索它。填充数组后,我用removeAll检查了删除的元素数,它始终为0。所以问题就在这之后。使用下面的解决方案,游戏仍然可以运行,在冻结之前调用shiftMaze数百次。我找到了解决方案。我在运行中调用了Updateiles,在paint中调用了paintTiles,因此它们会随机相互干扰。现在两者都被称为“内画”。我做了一个测试,称为shiftMaze 500000次,只得到了28个NPE,通过第二个答案中的检查得到了解决。
public void shiftMaze() {
    //<-- to do here
    tilearray.removeAll(Collections.singleton(null));//<-- at the end
}
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();