title: Serialized object has null values after being sent via UDP to client in java
body: <p>This problem is driving me up the wall. This is for a <em>very</em> simple online multiplayer game that I am currently working on.</p>
  
  <p>I am currently able to send packets via udp to my client(s), and they seem to receive them fine. However, when I send a serialized object to my client and deserialize at the other end, I'm getting NullPointerExceptions when I try to access the values I need. I have verified that the object is being correctly serialized on the server side (deserialized it and checked the data), so I am 99% sure I am doing something very wrong with my code for sending the packet.</p>
  
  <p>Here is the code for serializing and sending the "Datagram" object from the server:</p>
  
  <pre><code>    DatagramPacket sendPacket = null;
  
      byte[] buf = null;
  
      //Serialize the datagram object to send as a UDP packet
      try {
  
          // Serialize to a byte array
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          ObjectOutput out = new ObjectOutputStream(bos);   
          out.writeObject(data);
          buf = bos.toByteArray(); 
  
          out.close();
          bos.close();
  
      } catch (IOException e) {
      }
  
      try {
          sendPacket = new DatagramPacket( buf, buf.length,
                  InetAddress.getLocalHost(), 4004);
      } catch (UnknownHostException e){}
  
      try {
          DatagramSocket sendSocket = new DatagramSocket();
          sendSocket.send( sendPacket );
          changed = true;
      }catch (IOException e) {}
  </code></pre>
  
  <p>The "data" object being serialized is full of correct values; I am sure of this.</p>
  
  <p>The other relevant code block is the receiving block on the client side:</p>
  
  <pre><code>public Datagram readDatagram() {
  
      byte[] buff = new byte[20000];
      DatagramPacket packet = new DatagramPacket(buff, buff.length);
      DatagramSocket receiver = null;
  
      try {
          receiver = new DatagramSocket(4004);
          receiver.receive(packet);
      } catch (IOException e) {
          System.out.println("ERROR2");
      }
  
      Datagram data = null;// = new Datagram();
  
      try {
          // Deserialize from a byte array
          ByteArrayInputStream bis = new ByteArrayInputStream(buff);
          ObjectInput in = new ObjectInputStream(bis);
          data = (Datagram) in.readObject();
  
          bis.close();
          in.close();
      } catch (ClassNotFoundException e) {
      } catch (IOException e) {
          System.out.println("ERROR3");
      }
  
      for (int i = 0; i &lt; 35; i++) {
          System.out.print(data.getLevel()[i]);
      }
  
      receiver.close();
  
      return data;
  
  }
  </code></pre>
  
  <p>When I attempt to read any values after this deserialization, I get the NullPointerException. If someone can point me in the right direction I will be extremely happy.</p>
  
  <p>Oh, and I am sending to localHost right now intentionally just to test things out. My client and server are both running on my machine.</p>