MurphyP
February 10th, 2009, 02:17 AM
Hi guys, first time poster here, long time Ubuntu user
I am having problems transferring objects via UDP from one thread to another. I keep getting NullPointerException's on my send() call.
I am transferring a "Box" object that implements Serializable, it has two values one is another object that implements Serializable
Here is what my send looks like:
Box box = new Box(valueA,valueB);
InetAddress ip = InetAddress.getByName("localhost");
ByteArrayOutputStream b_out = new ByteArrayOutputStream();
ObjectOutputStream o_out = new ObjectOutputStream(b_out);
o_out.writeObject(box);
byte[] sStream = byteOut.toByteArray();
DatagramPacket sPacket = new DatagramPacket(sStream, sStream.length, ip, toPortNum);
socket.send(sPacket);
Here is what the receive looks like:
byte[] r_data;
DatagramSocket socket;
DatagramPacket r_packet;
ByteArrayInputStream b_in;
Box obj;
r_data = new byte[10000];
b_in = new ByteArrayInputStream(r_data);
r_packet = new DatagramPacket(r_data, r_data.length);
try {
socket.receive(r_packet);
ObjectInputStream o_in = new ObjectInputStream(b_in);
obj = (Box) o_in.readObject();
} catch (Exception e) {e.printStackTrace();}
the toPortNum is the same number as the receiving threads socket. I have printed the box object before I send it, so it isnt null. The receiving thread waits at .receive(r_packet) and Iget the NullPointerExcption at the sending thread .send(sPacket). I am very new to socket programming so it could be something trivial...
Any ideas?
Thanks!
Murph
I am having problems transferring objects via UDP from one thread to another. I keep getting NullPointerException's on my send() call.
I am transferring a "Box" object that implements Serializable, it has two values one is another object that implements Serializable
Here is what my send looks like:
Box box = new Box(valueA,valueB);
InetAddress ip = InetAddress.getByName("localhost");
ByteArrayOutputStream b_out = new ByteArrayOutputStream();
ObjectOutputStream o_out = new ObjectOutputStream(b_out);
o_out.writeObject(box);
byte[] sStream = byteOut.toByteArray();
DatagramPacket sPacket = new DatagramPacket(sStream, sStream.length, ip, toPortNum);
socket.send(sPacket);
Here is what the receive looks like:
byte[] r_data;
DatagramSocket socket;
DatagramPacket r_packet;
ByteArrayInputStream b_in;
Box obj;
r_data = new byte[10000];
b_in = new ByteArrayInputStream(r_data);
r_packet = new DatagramPacket(r_data, r_data.length);
try {
socket.receive(r_packet);
ObjectInputStream o_in = new ObjectInputStream(b_in);
obj = (Box) o_in.readObject();
} catch (Exception e) {e.printStackTrace();}
the toPortNum is the same number as the receiving threads socket. I have printed the box object before I send it, so it isnt null. The receiving thread waits at .receive(r_packet) and Iget the NullPointerExcption at the sending thread .send(sPacket). I am very new to socket programming so it could be something trivial...
Any ideas?
Thanks!
Murph