File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading an object from ObjectInputStream

 
Richard Robbins
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mark Savory
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try ObjectInputStream.readObject and cast the returned Object into your specific class type.
 
Richard Robbins
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic