简体   繁体   English

序列化对象通过UDP发送到Java中的客户端后具有空值

[英]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. 我目前能够通过udp向我的客户端发送数据包,并且它们似乎没问题。 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. 但是,当我将序列化的对象发送给客户端并在另一端反序列化时,尝试访问所需的值时会出现NullPointerExceptions。 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. 我已经验证了对象已在服务器端正确序列化(反序列化并检查了数据),因此我有99%的把握要对发送数据包的代码做错什么。

Here is the code for serializing and sending the "Datagram" object from the server: 以下是用于从服务器序列化和发送“ Datagram”对象的代码:

    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. 当我尝试在反序列化之后读取任何值时,会收到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. 哦,我现在有意发送到localHost只是为了进行测试。 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. 我敢打赌,正在接收结束代码中引发ClassNotFoundException This would leave you with data == null , and that would then lead to an NPE in the following loop. 这将使您的data == null ,然后将导致以下循环中的NPE。

One possible problem with your code is that you call toByteArray() before closing the ObjectOutputStream : 您的代码可能存在的一个问题是您在关闭ObjectOutputStream之前调用toByteArray()

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. 如果序列化数据的某些部分直到close()才写入输出流,在这种情况下它们将丢失。 Try to call toByteArray() after closing the streams. 关闭流后,尝试调用toByteArray()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过udp发送后,序列化对象的值为空 - Serialized object has null values after sending via udp 如何获取要序列化并发送到客户端的Java对象字段 - How to get Java object field to be serialized and sent to the client RMI:远程对象的字段是否已序列化并发送到客户端? - RMI: are fields of remote object serialized and sent to client? 如何使通过网络传递的Java序列化对象将更改通知客户端GUI - How to make a Java serialized object being passed over network notify client GUI of change 为 java 客户端使用 openapi 生成器时,通用 object 名称未序列化 - Generic object names not being serialized when using openapi generator for a java client .NET客户端构造的SOAP具有有效对象,Java客户端SOAP具有空对象 - SOAP constructed by .NET client has valid object, Java client SOAP has null object 序列化空值的大小 - Size of a serialized null values 为什么不将此空值序列化? - why is this null value not being serialized? 如何在 java 中从客户端发送到服务器后暂停特定字符串? - How to pause a specific string after it has been sent from client to server in java? java.lang.IllegalArgumentException:对象将被序列化为`null`:Android - java.lang.IllegalArgumentException: Object would be serialized to `null`: Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM