0

我正在尝试使用 Java,我有一个游戏读取的保存文件,其组成如下;在此处输入图像描述

0 是草纹理(这有效),但是当我将 0 更改为 1 以将草更改为石头时,它在第一个之后不起作用,就像我可以将它们更改为 1 一样,它可以正常工作,但其余部分不行。我坐了一会儿,试图找出问题所在,但找不到。我会更新更多信息。

处理块类型的代码是这样的;

public class Value {
    public static int groundGrass = 0;
    public static int groundRoad = 1;

    public static int airAir = -1;
}

这处理文件保存;

import java.io.*;
import java.util.*;

public class Save {
    public void loadSave(File loadPath){
        try{
            Scanner loadScanner = new Scanner(loadPath);

            while(loadScanner.hasNext()){
                for(int y=0;y<Screen.room.block.length;y++){
                    for(int x=0;x<Screen.room.block[0].length;x++){
                        Screen.room.block[y][x].groundID = loadScanner.nextInt();
                    }
                }

                for(int y=0;y<Screen.room.block.length;y++){
                    for(int x=0;x<Screen.room.block[0].length;x++){
                        Screen.room.block[y][x].airID = loadScanner.nextInt();
                    }
                }
            }

            loadScanner.close();
        } catch(Exception e) { }
    }
}

这处理不同的块类型;

import java.awt.*;

public class Block extends Rectangle{
    public int groundID;
    public int airID;

    public Block(int x, int y, int width, int height, int groundID, int airID){
        setBounds(x, y, width, height);
        this.groundID = groundID;
        this.airID = airID;
    }

    public void draw(Graphics g){
        g.drawImage(Screen.tileset_ground[groundID], x, y, width, height, null);

        if(airID !=Value.airAir){
            g.drawImage(Screen.tileset_ground[airID], x, y, width, height, null);
        }
    }
}

这应该是重要的部分。

我添加了 e.printStackTrace(); (在正确的地方?)

并在控制台中得到了这个;

java.lang.NullPointerException
    at com.humans.vs.technology.Save.loadSave(Save.java:14)
    at com.humans.vs.technology.Screen.define(Screen.java:39)
    at com.humans.vs.technology.Screen.paintComponent(Screen.java:47)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$700(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(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.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)

如果有帮助,请下载完整代码 - https://dl.dropbox.com/u/3531994/HvsT.zip

4

1 回答 1

1

添加e.printStackTrace到 中的异常处理程序loadSave,它产生以下输出:

java.lang.NullPointerException
          at com.humans.vs.technology.Save.loadSave(Save.java:14)
          at com.humans.vs.technology.Screen.define(Screen.java:39)
          at com.humans.vs.technology.Screen.paintComponent(Screen.java:47)
          ...

有问题的行是:

Screen.room.block[y][x].groundID = loadScanner.nextInt();

由于我们知道loadScanner之前没有null并且null之前没有(因为它在上面几行清楚地初始化,并且前 8 个地砖正确加载),我们认为异常可能是从Scene.room.block[y][x].groundID.

我们可以使用与我们过去作为候选人排除相同的逻辑来排除Screen.roomScreen.room.block排除参考。看来有些是。搜索 的初始化,我们发现,如下所示。nullloadScannerblock[y][x]nullRoom.blockRoom.define

public void define(){
    block = new Block[worldHeight][worldWidth];

    for(int y=0;y<block.length;y++){
        for(int x=0;x<block.length;x++){
            block[y][x] = new Block((Screen.myWidth/2) - ((worldWidth * blockSize)/2) + (x * blockSize), y * blockSize, blockSize, blockSize, Value.groundGrass, Value.airAir);
        }
    }
}

我希望你注意到,你的循环覆盖x,y在区间[0, block.length] ; block.length对应worldHeight,问题就清楚了:这个循环只初始化了block的前8x8。一旦我们尝试读取Screen.room.block[y][x].groundID块 @ 0, 8,我们发现它是默认值,null. ANullPointerException被抛出,它被您的异常处理程序捕获,并且保存文件的其余部分未被读取。初始化的 8x8 部分blocks保留groundID默认值,0对应于草:

  public static int groundGrass = 0;

起初,我怀疑这也会导致房间第一行仅前 8 个块的未完成渲染,因为循环Room.draw调用block[y][x].draw(g)......但是,您的循环条件在这里也同样被破坏。


现在,解决方案很简单。Room.define更正and中的循环条件Room.draw,例如

    for(int y=0;y<worldHeight;y++){
        for(int x=0;x<worldWidth;x++){
            block[y][x] = new Block((Screen.myWidth/2) - ((worldWidth * blockSize)/2) + (x * blockSize), y * blockSize, blockSize, blockSize, Value.groundGrass, Value.airAir);
        }
    }

对于Room.define,以及以下对于Room.draw

    for(int y=0;y<worldHeight;y++){
        for(int x=0;x<worldWidth;x++){
            block[y][x].draw(g);
        }
    }

您的代码现在生成以下内容: http ://puu.sh/YvAJ 不客气 :-)

于 2012-08-24T23:13:28.687 回答