如何解决在空对象引用上调用虚拟方法'void java.io.PrintWriter.write(java.lang.String)'的尝试

时间:2019-11-28 10:54:03

标签: java android

我尝试通过.net应用程序与android应用程序之间的TCP / IP连接进行连接(发送和接收数据)。 以及当我尝试将数据发送到服务器异常时:

  

E / DeviceAPI_RFIDWithUHF:startInventory()错误:-1   E / DeviceAPI_RFIDWithUHF:stopInventory()错误:-1   E / AndroidRuntime:致命例外:Thread-5339       程序:com.example.uhf,PID:16138       java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void java.io.PrintWriter.write(java.lang.String)”           在com.example.uhf.fragment.UHFReadTagFragment $ Thread3.run(UHFReadTagFragment.java:585)           在java.lang.Thread.run(Thread.java:818)

我的代码:

class Thread21 implements Runnable {
    public void run() {
        Socket socket;
        try {//"192.168.1.125"IP1
            socket = new Socket("192.168.153.1", 7800);
            output = new PrintWriter(socket.getOutputStream());
            input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            mContext.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                }
            });
            new Thread(new Thread2()).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Thread2 implements Runnable {//recieve
    @Override
    public void run() {
        while (true) {
            try {
                final String message = input.readLine();
                if (message != null) {
                    mContext.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            tv_count.setText(message);
                        }
                    });
                } else {
                    Thread myThred = new Thread(new Thread21());
                    myThred.start();
                    return;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
class Thread3 implements Runnable {//send
    private String message;
    Thread3(String message) {
        this.message = message;
    }
    @Override
    public void run() {
        output.write(message);//Line of problem
        output.flush();
        mContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {


            }
        });
    }
}

我需要知道这个问题。

0 个答案:

没有答案