• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EOF Exception

 
Greenhorn
Posts: 27
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone !
Whenever i write a program about Streams and serialization i faced EOF exception where i read Object

Object obj = null;
obj = br.readObject();
while(obj !=null)
{
...
...
obj = br.readObject();
}
Here compiler show me the error please explain .
 
Master Rancher
Posts: 4806
72
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, how are the objects serialized in the first place? My guess is, someone just serialized one or more objects, then closed the stream. You code, on the other hand, assumes that there will be a null reference sent. Apparently, there isn't one, and you get EOF instead.

Basically, both sender and receiver need to agree on a communication protocol. In this case, you've presumably agreed on what type of objects are being sent, but you don't know how to tell when the objects are done. There are several possibilities:

1. Agree on a special value that will be sent to indicate the end of your objects. Null is a good possibility. In that case, have the sender be sure and send a null, before they close the stream.

2. Before your list of objects, send an int representing the number of objects that will be sent. Then loop that number of times, and exit.

3. Wrap the objects in an array or list, and serialize that instead.

4. Just catch the EOFException and move on. That's the signal for the end of your processing.

Options 1-3 require the sender to change their code, as well as the receiver. You may or may not be able to do this. Options 2 and 3 require that you know how many objects there are before you send them. You may or may not be able to do this, as well. Personally I like option 1 best, but if you can't get the sender to change their code, option 4 may be your only option.
 
Salman Mushtaq
Greenhorn
Posts: 27
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i got the idea ,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic