我在从文件中读取对象时遇到问题。 我正在使用写作/阅读对象来保存和加载游戏 我有一个名为DataToSave的类,它的数据字段如下:
private int[][] XCoordinate = new int[Game.size][Game.size];
private int[][] YCoordinate = new int[Game.size][Game.size];
private boolean[][] isEpmty = new boolean[Game.size][Game.size];
private Piece[][] pieces = new Piece[Game.size][Game.size];
拥有自己的二传手和吸气剂。 这是我的保存和加载方法:
public static void saveObjectsToFile(Serializable data, File file) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
"C:\\Users\\Mohammad\\workspace\\Javafx\\Resources" + "\\" + file.getName(), true));
oos.writeObject(data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Object loadObjectsFromFile(File file) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
DataToSave newData = (DataToSave) ois.readObject();
ois.close();
return newData;
}
我的问题是当我从文件中读取对象时,我得到了pieces数组的NullPointerException。 但我可以打印它的数据字段 提前致谢
编辑: 这是我的SackTrace:
java.lang.NullPointerException
at Game.lambda$2(Game.java:721)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
编辑2: 这里是我的课程,没有不必要的方法:
public class Game extends Application {
static Place[][] labels = new Place[size][size];
static Weapon[] weaponForPick = new Weapon[4];
static Weapon[] weaponOfEnemy = new Weapon[4];
loadBtn.setOnAction(e -> {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("All Files (*.*)", "*.*");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.setInitialDirectory(new File("C:\\Users\\Mohammad\\workspace\\Javafx\\Resources"));
File file;
try {
file = fileChooser.showOpenDialog(primaryStage);
DataToSave data = (DataToSave) Manager.loadObjectsFromFile(file);
for (int i = 0; i < labels.length; i++) {
for (int j = 0; j < labels.length; j++) {
labels[i][j].x = data.getxCoordinate(i, j);
labels[i][j].y = data.getYCoordinate(i, j);
labels[i][j].Empty = data.getIsEpmty(i, j);
labels[i][j].piece = (Piece) data.getPieces(j, j);
}
}
for (int i = 0; i < labels.length; i++) {
for (int j = 0; j < labels.length; j++) {
if (!labels[i][j].Empty) {
if (labels[i][j].getPiece().type.compareTo("Soldier") == 0) {
if (labels[i][j].getPiece().color.compareTo("Blue") == 0) {
labels[i][j].setImage(new Image("images/BlueSoldier.jpg"));
labels[i][j].setGraphic(new ImageView(labels[i][j].getImage()));
} else {
labels[i][j].setImage(new Image("images/RedSoldier.jpg"));
labels[i][j].setGraphic(new ImageView(labels[i][j].getImage()));
}
}
if (labels[i][j].getPiece().type.compareTo("Commander") == 0) {
if (labels[i][j].getPiece().color.compareTo("Blue") == 0) {
labels[i][j].setImage(new Image("images/BlueCommander.jpg"));
labels[i][j].setGraphic(new ImageView(labels[i][j].getImage()));
} else {
labels[i][j].setImage(new Image("images/RedCommander.jpg"));
labels[i][j].setGraphic(new ImageView(labels[i][j].getImage()));
}
}
if (labels[i][j].getPiece().type.compareTo("Knight") == 0) {
if (labels[i][j].getPiece().color.compareTo("Blue") == 0) {
labels[i][j].setImage(new Image("images/BlueKnight.jpg"));
labels[i][j].setGraphic(new ImageView(labels[i][j].getImage()));
} else {
labels[i][j].setImage(new Image("images/RedKnight.jpg"));
labels[i][j].setGraphic(new ImageView(labels[i][j].getImage()));
}
}
} else {
labels[i][j].clear();
labels[i][j].setGraphic(null);
}
}
}
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
});
}
class Piece implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
int x;
int y;
Weapon[] weapons = new Weapon[4];
String color;
String type;
}
class Place extends Label implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
Piece piece;
boolean Empty = true;
int x;
int y;
transient Image image;
}
编辑3: 这是我在第二个if语句中得到异常的地方
if (!labels[i][j].Empty) {
if (labels[i][j].getPiece().type.compareTo("Soldier") == 0) {
if (labels[i][j].getPiece().color.compareTo("Blue") == 0) {
labels[i][j].setImage(new Image("images/BlueSoldier.jpg"));
labels[i][j].setGraphic(new ImageView(labels[i][j].getImage()));
} else {
labels[i][j].setImage(new Image("images/RedSoldier.jpg"));
labels[i][j].setGraphic(new ImageView(labels[i][j].getImage()));
}
}