Question

I'm completely new to programming, so I'm having a difficult time resolving my own errors. Someone advised me to try it on this website, so I thought why not give it a shot.

The other posts that I found regarding this error didn't seem very relevant: most were people advising to close the input stream, but my code already does that.

What I want it to do: Write a Photo object called "photo" to a file called "test.ser". Then read the file "test.ser" AND return the path of the object ("photo") in "test.ser" back to me.

What it actually does: Writes a Photo object called "photo" to "test.ser". Reads "test.ser", returns an EOFException and no path.

Returning the path isn't actually very important, as long as it returns something of value to me. But I am getting the same error when I use "System.out.println(photo)" or "photo.getId()".

I'm not very sure what I need to paste here, so I will post the two try/catch-es that I use for serializing and deserializing the object:

Serializing object:

    File test = new File("path.../something.ser");
    Photo photo = new Photo(2, "..\\images\\2.jpg", getImage("..\\images\\2.jpg"));

    try {
        FileOutputStream fos = new FileOutputStream(test);
        ObjectOutputStream out = new ObjectOutputStream(fos);

        if (!test.exists()) {
            test.createNewFile();
        }
        out.writeObject(photo);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

Deserializing object:

        try {
        FileInputStream fis = new FileInputStream(test);
        ObjectInputStream in = new ObjectInputStream(fis);

        in.readObject();

        photo = (Photo)in.readObject();
        photo.getPath();
        in.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

And the error:

run:
null
java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at view.Main.<init>(Main.java:103) 
  //103 is the line that casts the input object to a Photo object.

BUILD SUCCESSFUL (total time: 1 second)

From what I unstander the error occurs when I am trying to type cast the object - that I receive through the method readObject - to a "photo" class object. At least, that's what the error at line 103 is refering to.

I read elsewhere that the error means that I "tried to read more objects than there actually are there". Not sure what that means though, because I just want it to read 1 image - which should be within the object - and return its location.

Also I read that ObjectInputStream never returns null, unless I gave that value somewhere. But it actually is returning(?) "null", even though my code doesn't contain a null value...

I've been at it for days now (yes I am just that bad) and still no luck.

Was it helpful?

Solution

You read it twice:

    in.readObject();

    photo = (Photo)in.readObject();

Remove the first line. Also you don't have to create the file. The output stream will do that for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top