This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 1761
    
    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: 1761
    
    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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on ObjectStreams