我正在尝试使用 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