简体   繁体   中英

Serialized object has null values after being sent via UDP to client in java

This problem is driving me up the wall. This is for a very simple online multiplayer game that I am currently working on.

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.

Here is the code for serializing and sending the "Datagram" object from the server:

    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) {}

The "data" object being serialized is full of correct values; I am sure of this.

The other relevant code block is the receiving block on the client side:

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 < 35; i++) {
        System.out.print(data.getLevel()[i]);
    }

    receiver.close();

    return data;

}

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.

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.

On both the sending and receiving ends, you are catching and squashing exceptions. There is a good chance that this is hiding evidence that would help you diagnose the problem. Even if this is not the case, squashing exceptions like that is dangerous practice.

My bet is that a ClassNotFoundException is being thrown in the receiving end code. This would leave you with data == null , and that would then lead to an NPE in the following loop.

One possible problem with your code is that you call toByteArray() before closing the ObjectOutputStream :

out.writeObject(data);
buf = bos.toByteArray();

out.close();
bos.close(); 

If some parts of serialized data are not written into output stream until close() , they would be lost in this case. Try to call toByteArray() after closing the streams.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM