[英]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.