首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWT-EventQueue-1 NullExceptionPointer

AWT-EventQueue-1 NullExceptionPointer
EN

Stack Overflow用户
提问于 2014-08-30 06:43:53
回答 2查看 231关注 0票数 1

我在用瓷砖做迷宫游戏。迷宫每隔一段时间就会改变形状。在这个时候,在迷宫重建之前,铁射线就会变得空了。我得到NPE错误后,在不同的时间间隔和一段时间后,游戏停止。可能是因为有些方法在实际重新创建之前尝试访问。我尝试将if(.=null)放在某些地方,甚至尝试同步某些部分(我不太熟悉)。

编辑:谢谢Arvind指出了这个问题。我设法修复了它,但我仍然会学学更多的帮助,因为这不是最好和最整洁的修复,我认为。在某些情况下,Tile等于空。我只是简单地检查了它,并将其放入for循环中以避免错误。

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

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

代码语言:javascript
复制
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)

过了一段时间,它就会因为这个错误而冻结:

代码语言:javascript
复制
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)

下面是代码的一些部分:

代码语言:javascript
复制
@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类的:

代码语言:javascript
复制
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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-31 02:09:57

问题在于:

代码语言:javascript
复制
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并覆盖方法add()和addAll(),并使用null检查。

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

代码语言:javascript
复制
    tilearray.removeAll(Collections.singleton(null));

编辑

如果您正在寻找第二个选项,那么您应该尝试如下:

代码语言:javascript
复制
public void shiftMaze() {
    //<-- to do here
    tilearray.removeAll(Collections.singleton(null));//<-- at the end
}
票数 0
EN

Stack Overflow用户

发布于 2014-08-31 05:33:17

谢谢你的帮助,我试过了,但还是有些无效。同时,我做了以下工作,以消除错误。它几乎一直工作,但很少我仍然得到NPE或索引的范围外的错误.我加快了游戏到100毫厘娱乐,以获得错误检查在长期内。

代码语言:javascript
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25579769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档