简体   繁体   中英

Deserializing returns null object Java

Currently writing an application like Booking and I am in the stage where i want to store my infomation in files. I have created a Serializable Database class which has a field with a pathname and 2 methods for read/write. I have about 8 other classes which extend the Database and each holds a Hashmap and some querying methods. Naturally I read the Databases from my files before i start the application and i write before exiting, but I ran into a problem where the objects I am reading are all null . I've been at it for 2 hours now and I need a second opinion. This is the database's read/write methods:

public void write() {
        try {
            File temp = new File(this.filename);
            temp.createNewFile(); // create file if not present
            FileOutputStream fileOut = new FileOutputStream(this.filename);
            ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
            objectOut.writeObject(this);
            objectOut.close();
            System.out.println("The Object  was successfully written to a file");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public Object read() {
        Object obj = null;
        try {
            File temp = new File(this.filename);
            temp.createNewFile(); // create file if not present
            FileInputStream fileIn = new FileInputStream(this.filename);
            ObjectInputStream objectIn = new ObjectInputStream(fileIn);
            obj = objectIn.readObject();
            System.out.println("The Object was successfully read from the file");
            objectIn.close();
        } catch (EOFException ex) {
            return obj;
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

And here is the way I load them (Which is probably the problem) in my Application class

private void loadData() {
        accommodationReviewsDatabase = (AccommodationReviews) accommodationReviewsDatabase.read();
        brokerAccommodationsDatabase = (BrokerAccommodations) brokerAccommodationsDatabase.read();
        credentialsUserDatabase = (CredentialsUser) credentialsUserDatabase.read();
        customerReviewsDatabase = (CustomerReviews) customerReviewsDatabase.read();
        userConfirmationsDatabase = (UserConfirmations) userConfirmationsDatabase.read();
        userMessagesDatabase = (UserMessages) userMessagesDatabase.read();
    }

    private void writeData() {
        accommodationReviewsDatabase.write();
        brokerAccommodationsDatabase.write();
        credentialsUserDatabase.write();
        customerReviewsDatabase.write();
        userConfirmationsDatabase.write();
        userMessagesDatabase.write();
    }

Some extra information that may be asked:

  • All my classes that I am storing are serializable
  • The files I am storing the databases are all *.ser (Thats the extension I found)
  • The files are stored inside the project

If your read() method completes without an EOFException, it ends with return null; . You should return obj; , the object you read.

You should not expect that EOFException will be thrown if your read succeeds. The EOFException would indicate that it ran out of data while it was trying to read your object, and could not complete successfully.

If you do get an EOFException, it is probably a good idea to give some indication instead of silently returning. Silent catch blocks deny you information that could be useful for debugging.

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