Reputation: 35
sorry if this is a simple question, but i'm newbie in Java programming and can't deal with one problem with Java Save File Bug.. Bug is that, sometimes it saves and loads values without errors, some times it shows me error and still loads. But sometimes savefile wont work.
Error That I get :
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Clicker.ClickerGame.LoadFile(ClickerGame.java:2479)
at Clicker.mainClass.main(mainClass.java:27)
My Main (Starter Class) :
package Clicker;
import javax.swing.JFrame;
public class mainClass {
public static void main(String[] args) {
ClickerGame CG = new ClickerGame();
JFrame frame = new JFrame("CarCollectionarV1");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CG.addComponentToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
frame.setSize(1000, 700);
CG.LoadFile();
boolean GameLoop = true;
while(GameLoop){
CG.refresh();
CG.SaveFile();
}
}
}
ClickerGame class (Where are SaveFile and LoadFile functions):
public void SaveFile(){
try{
File SaveFile = new File("SaveFile.sav");
if(!SaveFile.exists()) {
SaveFile.createNewFile();
}
FileOutputStream saveFileSub = new FileOutputStream(SaveFile);
ObjectOutputStream save = new ObjectOutputStream(saveFileSub);
save.writeObject(CarMain.main);
save.writeObject(Box.boxes);
save.writeObject(CarFrame.frames);
save.writeObject(Part.parts);
save.writeObject(CarsLv1.cars);
save.writeObject(CarsLv2.cars);
save.writeObject(CarsLv3.cars);
save.writeObject(CarsLv4.cars);
save.writeObject(CarsLv5.cars);
save.writeObject(CarsLv6.cars);
save.writeObject(CarsLv7.cars);
save.writeObject(CarsLv8.cars);
save.writeObject(CarsLv9.cars);
save.writeObject(CarsLv10.cars);
save.close();
}
catch(Exception exc){
exc.printStackTrace();
}
}
public void LoadFile(){
try{
File SaveFile = new File("SaveFile.sav");
if(!SaveFile.exists()) {
SaveFile.createNewFile();
}
FileInputStream SaveFileSub = new FileInputStream(SaveFile);
ObjectInputStream load = new ObjectInputStream(SaveFileSub);
CarMain.main = (Integer[]) load.readObject();
Box.boxes = (Integer[]) load.readObject();
CarFrame.frames = (Integer[]) load.readObject();
Part.parts = (Integer[]) load.readObject();
CarsLv1.cars = (String[]) load.readObject();
CarsLv2.cars = (String[]) load.readObject();
CarsLv3.cars = (String[]) load.readObject();
CarsLv4.cars = (String[]) load.readObject();
CarsLv5.cars = (String[]) load.readObject();
CarsLv6.cars = (String[]) load.readObject();
CarsLv7.cars = (String[]) load.readObject();
CarsLv8.cars = (String[]) load.readObject();
CarsLv9.cars = (String[]) load.readObject();
CarsLv10.cars = (String[]) load.readObject();
load.close();
}
catch(Exception exc){
exc.printStackTrace();
}
}
Upvotes: 0
Views: 81
Reputation: 4662
You loadFile method is reading more than the file content. That's what an EOFException (End of File exception) is.
In your IDE, where you can see the line numbers, you'll be able to tell exactly at what point you overread: it's line 2479 in your ClickerGame class (see the stacktrace of the error)
If I were to guess, you are loading the file before saving it: since your loadfile creates the file if it's not there, you'll create an empty file which content then you try to read.
Upvotes: 2