-1
我是新手@Java編程。我一直在爲SaveFile工作,爲我的遊戲。加載並保存數據。我有2個以下錯誤:Java SaveFile將不起作用
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Clicker.LoadFile.<init>(LoadFile.java:17)
at Clicker.mainClass.main(mainClass.java:21)
java.lang.NullPointerException
at Clicker.SaveFile.<init>(SaveFile.java:17)
at Clicker.mainClass$1.run(mainClass.java:26)
at java.lang.Thread.run(Unknown Source)
我有MainClass,在那裏我把SaveClass和loadClass的:
package Clicker;
進口javax.swing.JFrame中;
公共類mainClass {
public static void main(String[] args) {
ClickerGame game = new ClickerGame();
JFrame frame = new JFrame("CarCollectionarV1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
frame.setSize(1000, 700);
LoadFile load = new LoadFile();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
SaveFile save = new SaveFile();
}
}));
}
完全 「SaveClass」 代碼:
package Clicker;
import java.io.*;
public class SaveFile{
ClickerGame CG = new ClickerGame();{
try{
File SaveFile = new File("SaveFile.sav");
if(!SaveFile.exists()) {
SaveFile.createNewFile();
}
FileOutputStream saveFileSub = new FileOutputStream(SaveFile);
ObjectOutputStream save = new ObjectOutputStream(saveFileSub);
save.writeObject(CG.CarMain.Money);
save.writeObject(CG.CarMain.MoneyClicks);
save.writeObject(CG.CarMain.Boxes);
save.writeObject(CG.CarMain.BoxesClicks);
save.writeObject(CG.CarMain.PlayerLevel);
save.writeObject(CG.CarMain.BoxLevel);
save.close();
}
catch(Exception exc){
exc.printStackTrace();
}
}
}
,充滿 「loadClass的」 代碼:
package Clicker;
import java.io.*;
public class LoadFile {
ClickerGame CG =new ClickerGame();{
try{
File SaveFile = new File("SaveFile.sav");
if(!SaveFile.exists()) {
SaveFile.createNewFile();
}
FileInputStream SaveFileSub = new FileInputStream(SaveFile);
ObjectInputStream load = new ObjectInputStream(SaveFileSub);
CG.CarMain.Money = (int) load.readObject();
CG.CarMain.MoneyClicks = (int) load.readObject();
CG.CarMain.Boxes = (int) load.readObject();
CG.CarMain.BoxesClicks = (int) load.readObject();
CG.CarMain.PlayerLevel = (int) load.readObject();
CG.CarMain.BoxLevel = (int) load.readObject();
load.close();
}
catch(Exception exc){
exc.printStackTrace();
}
}
設置您的代碼格式,併爲此網站使用正確的格式。此代碼不可讀,因此相當糟糕。 – bcsb1001
這個文件的'SaveFile.java:17'第17行究竟是什麼? –
也閱讀[Java編碼約定](http://www.oracle.com/technetwork/java/codeconventions-135099.html) –