• 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

Reading an object from ObjectInputStream

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try ObjectInputStream.readObject and cast the returned Object into your specific class type.
 
Richard Robbins
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic