我正在用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
1条答案
按热度按时间ergxz8rk1#
添加
e.printStackTrace
中的异常处理程序loadSave
,它产生以下输出:问题是:
既然我们知道
loadScanner
没有,没有null
但事实并非如此null
之前(因为它在上面的几行中就被明确初始化了,并且前8个地面片加载正确),我们认为异常可能是由Scene.room.block[y][x].groundID
.我们可以排除
Screen.room
以及Screen.room.block
从成为null
引用使用与我们用来消除loadScanner
作为候选人。似乎有些block[y][x]
是null
. 正在搜索的初始化Room.block
,我们发现Room.define
,见下图。我希望你注意到,你的环覆盖了
x
,y
在区间[0,block.length
];block.length
对应于worldHeight
,问题就清楚了:这个循环只初始化块的第一个8x8。一旦我们试图读懂Screen.room.block[y][x].groundID
比如说,块@0,8,我们发现它是默认值,null
. 一NullPointerException
抛出异常处理程序捕获的,而保存文件的其余部分未读。的8x8部分blocks
已经初始化了groundID
作为默认值,0
,对应于草:起初,我怀疑这也会导致房间第一排的前8个街区的未完成渲染,因为
Room.draw
电话block[y][x].draw(g)
在循环中。。。然而,循环条件在这里也同样被破坏。现在,解决办法很简单。更正中的循环条件
Room.define
以及Room.draw
,例如。为了
Room.define
,以及以下Room.draw
.您的代码现在生成以下内容:http://puu.sh/yvaj 不客气:-)