This discussion is archived
9 Replies Latest reply: Feb 28, 2011 6:08 PM by 843437 RSS

Error in deserialisation....

843437 Newbie
Currently Being Moderated
I am trying to serialize the object of my class locally as well as over the network to the server. When I try to retrieve the object by deserialization locally I get back the state of the object, but when I try to deserialize it over the network, one of the fields is null. The field which is turning out null is
  private List<Instruction> negativeExamples = new LinkedList<Instruction>();
I have marked the
 class Instruction 
as implementing the serializable interface. The portion of code which saves the object over the network is:
     public void storeObject(String key, Object object) throws IOException {
          
          String location = locationURL+"/Servlet/stores?cmd=storeObj&file="+key;
          URL servlet = new URL(location);
          URLConnection servletConnection = servlet.openConnection();
          
          servletConnection.setDoInput(true);
          servletConnection.setDoOutput(true);
          servletConnection.setUseCaches(false);
          servletConnection.setDefaultUseCaches(false);
          servletConnection.setRequestProperty("Content-Type", "application/octet-stream");
          
          //OutputStream output = servletConnection.getOutputStream();
          ObjectOutputStream output = new ObjectOutputStream(servletConnection.getOutputStream());
          
          output.writeObject(object);
          output.flush();
          output.close();          
          
          InputStream input = servletConnection.getInputStream();

          String dataString = "";
          int data = input.read();
          while(data != -1)
          {
               dataString = dataString + (char)data;
               data = input.read();
          }

          trace.out("miss", "Inside storeObject");
          trace.out("miss", "DataString:    " + dataString);
          input.close();
          //System.out.print(dataString);
     }
For further debugging I tried to get back the state of the object that I wrote to the server immediately after I have written the object and even the field for that
object is turning out to be null.

The code which is used to get back the object from the server is:
     public Object retrieveObject(String key) throws IOException {
          String location = locationURL+"/Servlet/stores?cmd=retrieveObj&file="+key;
          URL servlet = new URL(location);
          URLConnection servletConnection = servlet.openConnection();
          
          servletConnection.setUseCaches(false);
          servletConnection.setDefaultUseCaches(false);
          
          ObjectInputStream inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());
          
          Object object = null;
          try {
               object = inputFromServlet.readObject();
          } catch (ClassNotFoundException e) {
               e.printStackTrace();
          }
          
          // Close the stream
          inputFromServlet.close();
          return object;
     }
What can be the reason for it? I know that the thing works fine when I save it to a file locally and then retrieve it back but this thing over the network is turning out to be well something mysterious. Any help or suggestions would be appreciated.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points