This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Doubt on ObjectStreams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Doubt on ObjectStreams" Watch "Doubt on ObjectStreams" New topic
Author

Doubt on ObjectStreams

Anand Damodaran
Greenhorn

Joined: Oct 04, 2012
Posts: 21
Hi,

why do i get null message in the following code?

Output:
Student{name=anand, age=25}
Student{name=damodaran, age=20}
null



Kindly help me...

with respect,
Anand
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1756
    
    7

The way you invoke readObject() is guaranteed to throw an EOFException:
ObjectInputStream Javadoc wrote:
Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true. Non-object reads which exceed the end of the allotted data will reflect the end of data in the same way that they would indicate the end of the stream: bytewise reads will return -1 as the byte read or number of bytes read, and primitive reads will throw EOFExceptions.

My bet is that the the getMessage() call in your catch block returns null. Try adding ex.printStackTrace(); to the catch block to find out.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Anand Damodaran
Greenhorn

Joined: Oct 04, 2012
Posts: 21
Jelle Klap wrote:The way you invoke readObject() is guaranteed to throw an EOFException:
ObjectInputStream Javadoc wrote:
Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true. Non-object reads which exceed the end of the allotted data will reflect the end of data in the same way that they would indicate the end of the stream: bytewise reads will return -1 as the byte read or number of bytes read, and primitive reads will throw EOFExceptions.

My bet is that the the getMessage() call in your catch block returns null. Try adding ex.printStackTrace(); to the catch block to find out.



Hi,

Can you alter the code to read the exact two object without any exception?


Kindly help me..


Regards,
Anand
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1756
    
    7

Sure, you could simply make two successive invocations of the readObject() method, instead of looping. The same way you invoke writeObject().
By the way, don't forget to properly close your streams! As of Java 7 the easiest way would be to use the try-with-resources approach.
 
 
subject: Doubt on ObjectStreams
 
Similar Threads
how to send parameters form applet to servlet
Serialization question
passing parameters form applet to servlet
Problem with ObjectInputStream
Serilization issue in string object ?