2 u012751110 u012751110 于 2016.04.30 18:35 提问

Android 中new ObjectInputStream无法创建对象 2C
//程序在这里报错了;
 this.ois = new ObjectInputStream(this.socket.getInputStream());
程序报错说空指针异常,表面上看是下面的第二条语句in在调用方法时出错的,实际上是Client.getInstance().getClientInputThread()返回了空对象,也即是ClientInputThread构造方法中的
this.ois = new ObjectInputStream(this.socket.getInputStream());
未能成功创建ois对象
这在后面打印出来的log信息也可以看出来。打印信息显示ois == null是 true,而oos == null 是 false;

事故语句块之语句1  final ClientInputThread in = Client.getInstance().getClientInputThread();
事故语句块之语句2  in.setMessageListener(new MsgListener(...){}); 

//输入线程
 public class ClientInputThread  extends Thread 
{

    @SuppressWarnings("unused")
    private Socket socket;
    private ObjectInputStream ois;
    private TransEntity transEn;
    private MsgListener msgListener;
    private boolean isStart = true;
    public ClientInputThread(Socket socket) 
    { 

        this.socket = socket;
        try
        {
            this.ois = new ObjectInputStream(this.socket.getInputStream());
        } 
        catch (StreamCorruptedException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
    public TransEntity getMsg() 
    {
        if(transEn!=null)
        {

            synchronized (this) 
            {
                    try
                    {
                        wait();
                    } 
                    catch (InterruptedException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

            }

            return transEn;



        }
        return null;

    }


    public void setMessageListener(MsgListener msgListener) 
    {
        this.msgListener = msgListener;
    }

    public void releaseResource()
    {
        try 
        {
            ois.close();
            socket.close();
        } 
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void run()
    {
        while(isStart)
        {
            //if(ois == null)
                System.out.println("ois == null  : "+(ois == null));
            try 
            {
                transEn = (TransEntity) ois.readObject();
                if(transEn!=null)
                {
                    synchronized (this) 
                    {
                        notify();
                    }
                }

            //  msgListener.Message(transEn);
            } 
            catch (OptionalDataException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (ClassNotFoundException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        }
        releaseResource();
    }

}

 //输出线程
public class OutputThread extends Thread
{
        private Socket socket;
        private OutputThread  outputThread; 
        private ObjectOutputStream oos;
        private TransEntity transEn;
        private boolean isStart = true;
        public OutputThread(Socket socket)
        {
            this.socket = socket; 
            try 
            {
                oos = new ObjectOutputStream(socket.getOutputStream());
            } 
            catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }  
     // 设置了消息之后,唤醒写线程
        public void setMessage(TransEntity transEn) 
        {
            this.transEn = transEn;
            synchronized (this)
            {
                notify();
            }
        }
        public void releaseResouce()
        {
            try 
            {
                if (oos != null)
                oos.close();
                if (socket != null)
                socket.close();
            } 
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public void run()
        {
            while(isStart)
            {
            synchronized (this) 
            {
                try 
                {
                    if (transEn != null) 
                    {
                            wait();
                            try 
                            {
                                oos.writeObject(transEn);
                                oos.flush();
                                //oos.reset();
                            } 
                            catch (IOException e) 
                            {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                    }
                } 
                catch (InterruptedException e) 
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            }
            releaseResouce();
        }
}

//客户端线程,在客户端线程启动输出和输入线程
 public class  ClientThread extends Thread 
{

        private ClientOutputThread out;
        private ClientInputThread in;
        private Socket socket;  
        public ClientThread(Socket socket) 
        {
            this.socket = socket;


        }

        public void run() 
        {   

            out = new ClientOutputThread(socket);
            in = new ClientInputThread(socket);
            out.start();
            in.start();

        }

        public ClientInputThread getIn()
        {

            return in;

        }
        public ClientOutputThread getOut()
        {

            return out;

        }
}
//客户端
 public class Client
{
        private static Client client; 
      //  private boolean isLogin  = false;
        private Socket socket;
        private ClientThread clientThread;
        private Client()
        {

        }

        public static synchronized Client getInstance() 
        { 

                if (client == null) 
                {  
                    client = new Client();  
                }  
                return client;  
        }  

        public boolean startClient() 
        {
            try 
            {
                socket = new Socket();
                socket.connect(new InetSocketAddress("10.0.2.2",5345),2000);
                if (socket.isConnected()) 
                {
                    System.out.println("Connecting...");
                    clientThread = new ClientThread(socket);
                    clientThread.start();
                }
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
                return false;
            }
            return true;
        }

     // 直接通过client得到读线程
        public ClientInputThread getClientInputThread() 
        {
            return clientThread.getIn();
        }

        // 直接通过client得到写线程
        public ClientOutputThread getClientOutputThread() 
        {
            return clientThread.getOut();
        }

}
//在onResume里头启动service,由service启动客户端
     protected void onResume() 
    {
        // TODO Auto-generated method stub
        super.onResume();
        if (!GetMsgService.isStart) 
        {
             Intent service = new Intent(FirstActivity.this, GetMsgService.class);
             startService(service);
            //isServiceRunning = true;
        } 

    }

报错信息

报错信息说是空指针异常经过检查得知是ClinetInputThread的构造方法中的this.ois对象为成功创建,所以后来在通过Client的getClientInputThread方法得到的是一个空对象

但是ClientOutputThread中却能成功创建this.oos对象,如打印出的log信息显示,oos == true 是false
ClientInputThread未能成功创建this.ois对象,如打印出的log信息显示,ois == true是 true

3个回答

devmiao
devmiao   Ds   Rxr 2016.05.02 05:15
u010076059
u010076059   2016.05.04 22:03

this.ois = new ObjectInputStream(this.socket.getInputStream()); this.socket.getInputStream()==null 出现的null指针吧

u012751110
u012751110 this.socket.getInputStream()为空
一年多之前 回复
q610098308
q610098308   2016.04.30 21:59

this.ois = new ObjectInputStream(this.socket.getInputStream());
你传进来的 socket 有问题;

u012751110
u012751110 this.ois = new ObjectInputStream(socket.getInputStream());//这样写,一样不对啊
一年多之前 回复
u012751110
u012751110 怎么讲,为啥输出流没出错呢
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!