簡體   English   中英

AWT-EventQueue-1 NullExceptionPointer

[英]AWT-EventQueue-1 NullExceptionPointer

我正在使用瓷磚制作迷宮游戲。 迷宮會定期改變形狀。 這時,在迷宮重新創建之前,tilearray將變空。 在不同的間隔后以及游戲停止一段時間后,我收到NPE錯誤。 可能是因為某些方法在實際重新創建之前嘗試進行訪問。 我試圖將if(...!= null)放在某些地方,甚至試圖同步某些部分(我不太熟悉)。

編輯:感謝您Arvind指出問題。 我設法修復了它,但我仍然會尋求更多幫助,因為它不是我認為的最佳,最簡潔的修復方法。 在某些情況下,t等於零。 我只是檢查了一下,然后繼續。 進入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);
}

問題在這里:

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
}

謝謝您的幫助,我嘗試了其中的一個,但仍然會出現一些空值。 同時,我進行了以下操作以消除錯誤。 它一直都能正常運行,但是很少我仍然會得到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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM