Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading