Reputation: 19
This is a method to read show details from a .ser file containing serialized objects of type Show. The method successfully return the list but gives an Exception before. Why is it so and how do I get rid of it?
public List<Show> populateDataFromFile(String fileName) {
List<Show> shows=new ArrayList<Show>();
ObjectInputStream obj=null;
try {
FileInputStream fin=new FileInputStream(fileName);
obj=new ObjectInputStream(fin);
Show show=null;
while((show=(Show) obj.readObject())!=null)
{
shows.add(show);
show.getShowName();
}
System.out.println(shows);
} catch (IOException e) {
e.printStackTrace();
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}finally
{
try {
obj.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return shows;
}
The output is
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 com.util.DataManagerImpl.populateDataFromFile(DataManagerImpl.java:23)
at com.psl.Client.main(Client.java:9)
Show Name: Sahi re Sahi
Show Time: 6:30 PM
Seats Available: 40
Show Name: Ek Shyam Aapke Naam
Show Time: 6:30 PM
Seats Available: 40
Show Name: Moruchi Maushi
Show Time: 6:30 PM
Seats Available: 40
Show Name: All The Best
Show Time: 6:30 PM
Seats Available: 40
Show Name: Naksharanche Dene
Show Time: 6:30 PM
Seats Available: 40
The main method is
public static void main(String[] args) {
DataManager dm=new DataManagerImpl();
List<Show>shows=dm.populateDataFromFile("ShowDetails.ser");
// Call all the functionalities from here to test your code.
for(Show show:shows)
{
System.out.println("Show Name: "+show.getShowName());
System.out.println("Show Time: "+show.getShowTime());
System.out.println("Seats Available: "+show.getSeatsAvailable());
}
}
Upvotes: 2
Views: 24761
Reputation: 310883
Because you've reached the end of the file.
You seem to think that readObject()
returns null at end of file. It doesn't. It returns null if and only if you wrote a null.
The correct test is to catch EOFException
and break, not to read until readObject()
returns null.
Upvotes: 4
Reputation: 1563
EJP is correct that the error happens because the method readObject()
does not return NULL when there is no more Show in the file.
EJP also has suggested a solution. I just want to suggest several other solutions, but instead of fixing the code that read the file, the solutions fix the code that write the file.
Currently, the file is generated by calling writeObject()
method multiple times, each with a Show
object. Instead of doing this you can:
Create a List
object that holds the shows. Write this List
into file. When you read this file, you can just read the List
back. You don't have to use while loop, and therefore, avoid the EOF exception check.
Write the number of shows first to the file. After that, write the shows, one by one, to the file. When you read the file, you need to first read the number of shows, which informs you how many shows to read from the file. This will avoid the EOF exception check.
Upvotes: 2