日期:2014-05-20  浏览次数:20830 次

关于StreamConnection的问题
Java代码如下
 
Java code

public class SocketConn {
    public StreamConnection conn;
    public DataOutputStream outputStream;
    public DataInputStream inputStream;

    public SocketConn() {
    try {
        if (null == conn) {
        conn = (StreamConnection) Connector.open("socket://222.90.231.60:22616", Connector.READ_WRITE);

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
}
public class RequestDataImpl extends SocketConn implements IRequestData{
  //requestKeys()这个方法是没问题的,能正常发送、接收数据
  public void requestKeys() { 
    try { 
        outputStream = conn.openDataOutputStream();
        outputStream.write(getConvert().hexStringToBytes(REQKEYSTR));
        outputStream.flush();
        inputStream = conn.openDataInputStream();
       // ...
    } catch (IOException e) {
    }
    }
    public void loginSer() { 
    try { 
     [color=#FF0000]       //[b]问题:[/b]在conn.openDataOutputStream()的时候抛空指针(nullProintException)异常[/color]
        outputStream = conn.openDataOutputStream();
        outputStream.write("login");
        outputStream.flush();
        inputStream = conn.openDataInputStream(); 
            //...
    } catch (IOException e) {
    }
    }
}



如下运行 :
Java code
IRequestData reqData = new RequestDtaImpl();
reqData.requestKey();
reqData.loginSer();
请问各位,为什么在loginSer()方法中的conn.openDataOutputStream()会出现异常?
 

------解决方案--------------------
你确定你在requestKey()方法中没有把conn置为null?