简体   繁体   中英

NullPointerException when de-serializing a saved object ?

I input instructions using command line arguments. I pass the location of where the text file which contains info on how to run the class is present . In one instance the program has to act like it has an error and it needs to shut down. I am able to do this. the object is also serlized in the end. Now i have to use the same ser file to restart the program and start it from the same place as it last shut down using the restore class. For some odd reason i am getting a nullpinterException . I have marked where i am getting that error.

public static void main(String[] args) throws Exception {

    try {

        String option = args[0];
        String filename = args[1];


        if (!(option.equals("-f")) && !(option.equals("-d"))) {
            System.out.println("Invalid option");
            printUsage();
        }

        System.out.println(filename);
        GreenhouseControls gc = new GreenhouseControls();

        if (option.equals("-f")) {
            gc.addEvent(gc.new Restart(0, filename));
        try {
                        FileOutputStream fileOut = new FileOutputStream("/Users/Arsalan Khan/Google Drive/cosc/TME/src/dumpout.ser");
                        ObjectOutputStream out = new ObjectOutputStream(fileOut);
                        out.writeObject(gc);
                        out.close();
                    } catch (IOException i) {
                        i.printStackTrace();
                    }
        }
        gc.run();

        // serialization try catch


        // the instance where the error occored is also passed to
        if (option.equals("-d")) {

        // GreenhouseControls.main(GreenhouseControls.java:567) 
            Restore re = new Restore(filename);


        }

    }

    catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("Invalid number of parameters");
        printUsage();
        e.printStackTrace();
    }

}

public class Restore {
String fileNameNew;

public Restore(String fileName) {

    this.fileNameNew = fileName;

}

// print the state of the save to the console
{// deserilize dump.out
    try {
        // here at Restore.<init>(Restore.java:17)
        FileInputStream fileIn = new FileInputStream(fileNameNew); 
        ObjectInputStream in = new ObjectInputStream(fileIn);

        // it has to be cast to GreenhouseControls since everything saved in
        // a file is retrieved as an object
        GreenhouseControls readGreen = (GreenhouseControls) in.readObject();
        in.close();
        System.out.println(readGreen);
        // since we are trying to fix the error in gc we will pass its
        // instance and use fixable to fix it
        readGreen.getFixable((Integer) readGreen.errorCode);
    } catch (IOException | ClassNotFoundException i) {
        i.printStackTrace();
    }
}

// restart from where it left the program
// run fix window and power on to fix problems

;

}

An exception at that point can only because fileNameNew is null . And it won't be thrown there. It must be thrown in the constructor chain, or in some method that it calls.

The problem is happening before you attempt to deserialize anything, and is (actually) nothing to do with the deserializion process.

In fact, the reason that fileNameNew is null is that you are attempting to do stuff in an instance initializer block. (Bad idea ...) That block gets executed before the Restore constructor body, and at that point, the fileNameNew field will still be in its default initialized state.

The solution is to put the code that is in the instance initializer block inside the constructor, so that it executes after this.fileNameNew = fileName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM