Q. One Q. One - 2 months ago 16
Java Question

Loop the deserialization is performed only once

I want to load a loop objects to the list, but I have a problem, because the loop is performed only once and the next loop crashes IOException line 'movie = (Movie) ois.readObject () ;'.

@SuppressWarnings("unchecked")
static <T> void loadDatabase(List<T> tab, int index) {
if(index == 1) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("movies.ser"));

try {
while(true) {
Movie movie = new Movie();
movie = (Movie) ois.readObject();
tab.add((T) movie);
}
} catch(EOFException ignored) {
ois.close();
}

} catch (FileNotFoundException e) {
System.out.println("Can not find the file. Please try again later...");
} catch (IOException e) {
System.out.println("Unable to save file. Please try again later...");
} catch (ClassNotFoundException e) {
System.out.println("The error of the projection class Movie");
}
} else if(index == 2) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("series.ser"));

while(ois.available() != 0) {
Series series = new Series();
series = (Series) ois.readObject();
tab.add((T) series);
}
ois.close();

} catch (FileNotFoundException e) {
System.out.println("Can not find the file. Please try again later...");
} catch (IOException e) {
System.out.println("Unable to save file. Please try again later...");
} catch (ClassNotFoundException e) {
System.out.println("The error of the projection class Series");
}
}
}

Answer

Lets dissect your (sorry, but horrible, horrible) code a bit:

@SuppressWarnings("unchecked")
static <T> void loadDatabase(List<T> tab, int index) {
  if(index == 1) { 

Dont do that. The whole point of index seems to be to distinguish what this method should do. Hint: create two methods instead; and avoid that parameter, and the if!

One method you would call loadMovieFromDataBase(); the other loadSeriesFromDataBase().

    try {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("movies.ser"));
        try {
        while(true) {

Do you really want to loop forever?! That is what while(true) will. Well, not forever, but until some exception will be thrown. So, even when your loop body would be doing the right thing (which it isnt!) ... that code must lead into some error condition.

            Movie movie = new Movie();
            movie = (Movie) ois.readObject();

That is like absolute nonsense. There is no point in calling new on a Movie object to have then read by an ObjectInputStream.

Movie movie = (Movie) ois.readObject();

is how you deserialize an object from an ObjectInputStream.

            tab.add((T) movie);
        }
        } catch(EOFException ignored) {
            ois.close();

Hint: you are a beginner. You have no idea what your code is doing; but you feel confident enough to ignore exceptions; not even trace them?!

And the following catches are not much better: } catch (FileNotFoundException e) { System.out.println("Can not find the file. Please try again later..."); } catch (IOException e) { System.out.println("Unable to save file. Please try again later...");

You try to read a file. Hint: be careful when doing copy&paste of ERROR messages. This one doesn't make any sense here. Beyond that: don't replace the exception with some self-defined message. At least print the exception message, too. Because: your own code is all lying.

    } catch (ClassNotFoundException e) {
        System.out.println("The error of the projection class Movie");
    }

No, the above error would mean that you tried to deserialize a class which does not exist in the context of the JVM that this code is running it.

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("series.ser"));

        while(ois.available() != 0) {
            Series series = new Series();
            series = (Series) ois.readObject();

Same story as above. And hint: don't query the stream if more input is there. Just serialize one list of Series objects; than you just de-serialize one object later on.

The reason why I spent my time here: it seems that you are blindly putting together code without the slightest idea what that code is doing. That is, well a bad idea.

You better step back and read & run good tutorials on serialization. And only when you are able to run and understand that part, you try to come up with your own code.

Comments