banq,你好!我做了自己的Chat,但调试时发现sendrequest()出错

05-08-23 gmslzm
banq,你好!我做了自己的Chat,但调试时发现sendrequest()出错,错误如下:

05-8-23 10:35:57 [Debug:com.jdon.jserver.connector.SocketDataHandler:Error] sendRequest() error:java.lang.NullPointerException

05-8-23 10:35:57 [Debug:com.jdon.jserver.connector.udp.UDPClient:Error] run error:java.lang.Exception: java.lang.NullPointerException

是这样的,我自己试着建立了一个对象:

public final class MsgObj {

public String phoneNo="";

public String msgInfo="";

public String msgType="";

public MsgObj() {

}

}

然后根据对象又建立了"被访问者"如下:

public class MsgObjType implements Linkable {

private MsgObj content=null;

private int msgType;

private ByteBuffer byteBuffer=null;

public MsgObjType(int msgType) {

this.msgType=msgType;

}

public MsgObj getContent(){

return content;

}

public void setContent(MsgObj content){

this.content=content;

}

public void accpet(QueueWorker worker) throws Exception {

worker.run(msgType,this);

}

public OutputStream getOutputStream() {

OutputStream outputStream=null;

try {

outputStream = DataTypeHelper.writeObject(content);

}

catch (Exception ex) {

ex.printStackTrace();

}

return outputStream;

}

public void setInputStream(InputStream in) {

try {

this.content = (MsgObj)DataTypeHelper.getObject(in);

}

catch (Exception ex) {

ex.printStackTrace();

}

}

}

在向服务器端发送时就发生了以上错误,难道MsgObj需要Serializable吗?请指教,谢谢!

banq
2005-08-23 16:51
多谢,你可能需要单点调试,具体看不出来.

gmslzm
2005-08-23 17:18
我,发送String类型的测试没有问题,但发送MsgObj就会出现以下异常:

java.lang.Exception: java.lang.NullPointerException

at com.jdon.jserver.connector.SocketDataHandler.sendRequest(SocketDataHandler.java:48)

at com.jdon.jserver.connector.udp.UDPClient.doKey(UDPClient.java:94)

at com.jdon.jserver.connector.udp.UDPClient.doSelector(UDPClient.java:73)

at com.jdon.jserver.connector.udp.UDPClient.run(UDPClient.java:57)

at java.lang.Thread.run(Thread.java:536)

Caused by: java.lang.NullPointerException

at com.jdon.jserver.connector.SocketDataHandler.sendRequest(SocketDataHandler.java:45)

... 4 more

我查看源程序,当我发送信息时,发现是下面这一句outByte取到的值是null,

ByteArrayOutputStream outByte = (ByteArrayOutputStream) messageQueue.removeReqFirst();

banq老师可否随便做一个SampleObj做为传输对象的例子,以供参考,我实在是找不到原因了,除了添加一个MsgObj及与其对应的MsgObjType类之外还要添加其它的代码吗?请指教,谢谢!

banq
2005-08-24 15:09
Object必须是可以序列化的Object

猜你喜欢