-1

I'm novice @Java Programming. I have been working for SaveFile, for my game. Load and Save data. I got 2 following errors:

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)

I have got MainClass, where i put in SaveClass and LoadClass :

package Clicker;

import javax.swing.JFrame;

public class 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();
        }
    }));
 }

Full "SaveClass" code:

  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();
    }
    }
}

And Full "LoadClass" code:

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();
            }
            }
crelix
  • 35
  • 6
  • Format your code, and use the correct formatting for this website. This code is unreadable and, consequentially, rather awful. – bcsb1001 Mar 07 '16 at 16:28
  • What is exactly at `SaveFile.java:17` line 17 of this file? – Jorge Campos Mar 07 '16 at 16:29
  • Also read about [Java Coding Conventions](http://www.oracle.com/technetwork/java/codeconventions-135099.html) – Jorge Campos Mar 07 '16 at 16:30
  • @JorgeCampos "save.writeObject(CG.CarMain.Money);" – crelix Mar 07 '16 at 16:31
  • So either `CG` is null or `CarMain` is null ! you have to check it before use it. – Jorge Campos Mar 07 '16 at 16:32
  • @Jorge Campos I have class in "CG" - class CarMain { public int Money = 0; public int Boxes = 0; public int MoneyClicks = 0; public int BoxesClicks = 0; public int PlayerLevel = 0; public int BoxLevel = 0 ; } – crelix Mar 07 '16 at 16:34
  • Just to have a class doesn't mean that it is initialized, here, read this article: [Creating Objects](https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html) – Jorge Campos Mar 07 '16 at 16:36
  • @JorgeCampos Thanks, I will read it. – crelix Mar 07 '16 at 16:37
  • @JorgeCampos It's possible to write private message? I readed, and remembered that i have read this one before. But I can't figure out, how to deal with this problem. For almost 3 weeks.. It would be very nice, if you could help me, with example. – crelix Mar 07 '16 at 16:53
  • No it is not. Try to check the answer in the question that your's get marked as duplicated. You may find it useful – Jorge Campos Mar 07 '16 at 16:59

1 Answers1

0

From what I gather your CG.CarMain.Money isn't defined when called in the context of SaveFile which causes the NullPointerException and an abortion of the file write. From then you have a corrupt file which LoadClass tries to read, only to raise EOFException.

Aaron
  • 24,009
  • 2
  • 33
  • 57