my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes Reading an object from ObjectInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading an object from ObjectInputStream" Watch "Reading an object from ObjectInputStream" New topic
Author

Reading an object from ObjectInputStream

Richard Robbins
Ranch Hand

Joined: Mar 07, 2001
Posts: 66
I am trying to send an Object from a client to a server via a socket connection. How does one read the Object from the ObjectInputStream on the server side?


Richard Robbins is one of the founders of WebsiteTemplateDatabase.com, a resource for free website templates and Photoshop tutorials. He also consults for an online boutique selling baptism dresses and christening gowns.
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Try ObjectInputStream.readObject and cast the returned Object into your specific class type.
Richard Robbins
Ranch Hand

Joined: Mar 07, 2001
Posts: 66
I keep getting a NullPointerException exception when I try to read the object. Here is the code:
inbound = new ObjectInputStream(recTask.getInputStream());
message = (InCMsg)inbound.readObject();
"inbound" is an ObjectInputStream object, and "message" is of type InCMsg (a class I created).
The client sends the object using:
toStoreMsg = new ObjectOutputStream(connection.getOutputStream());

//send message to output stream to be received by StoreMsg
toStoreMsg.writeObject(message);
Here, toStoreMsg is an ObjectOutputStream object and message is of type InCMessage.
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
I guess either inbound = new ObjectInputStream(recTask.getInputStream()) returns null or recTask.GetInputStream() returns null. I can't know why, of course.
prasad bhn
Greenhorn

Joined: May 04, 2001
Posts: 28
hi richard,
i think you have to get a Socket object by accepting the object sent by clinet at your serverside, using accept() method of ServerSocket.
after that, you need to get the InputStream using that socket object like
InputStream ins=socketobject.getInputStream();
using the ins object, you have create the ObjectInputStream like this.
ObjectInputStream ois=new ObjectInputStream(ins);
and after that....its very easy to solve ur problem...
HTH
Originally posted by Richard Robbins:
I am trying to send an Object from a client to a server via a socket connection. How does one read the Object from the ObjectInputStream on the server side?


prasad bhuvanam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading an object from ObjectInputStream
 
Similar Threads
error sending serialized object between client and server using sockets
Deserialization issue if I have serialized object to ByteArrayOutputStream
Determining if end of ObjectInputStream has been reached
StreamCorruptedException
How to cast an Object to a HashMap with out getting unchecked cast warning