Java 初始化后,对象为空

Java 初始化后,对象为空,java,android,Java,Android,我不知道为什么在运行时收到blow-posted错误,它说“试图调用空引用上的方法”,尽管对象mRFCOS被赋值 代码中有什么错误 正在运行(): private class RxRun implements Runnable { private BluetoothSocket mRFCSocket = null; private OutputStream mRFCOS = null; private InputStream mRFCIS = null; pu

我不知道为什么在运行时收到blow-posted错误,它说“试图调用空引用上的方法”,尽管对象mRFCOS被赋值

代码中有什么错误

正在运行()

private class RxRun implements Runnable {

    private BluetoothSocket mRFCSocket = null;
    private OutputStream mRFCOS = null;
    private InputStream mRFCIS = null;

    public RxRun() {
        mSPPCtrl = new SPPCtrl(getApplicationContext());
    }

    @Override
    public void run() {

        //initiating connection
        BluetoothSocket rfcSocket = mSPPCtrl.rfcConnect();

        if (rfcSocket.isConnected()) {
            Message msg = mHandler.obtainMessage(1);
            Bundle b = new Bundle();
            b.putString("CONNECTED", "RFC-SOCKET CONNECTED");
            msg.setData(b);
            mHandler.sendMessage(msg);
            //assigning stream variables
            try {
                this.mRFCOS = rfcSocket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                this.mRFCIS = rfcSocket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            Message msg = mHandler.obtainMessage(0);
            Bundle b = new Bundle();
            b.putString("DISCONNECTED", "RFC-SOCKET NOT CONNECTED");
            msg.setData(b);
            mHandler.sendMessage(msg);
        }

        //send a command to RFC-socket
        try {
            this.mRFCOS.write(DIRON.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        //read received response from RFC-socket
        int readBytes;
        try {
            while (this.mRFCSocket.isConnected() && (readBytes = this.mRFCIS.read(new byte[5120])) != -1) {
                Log.d(TAG, CSubTag.bullet("RxRun", "readBytes:" + readBytes));
                //mtvRx.setText("" + readBytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime: FATAL EXCEPTION: Thread-20426
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime: Process: com.example.com.rfcomm_test_00, PID: 11833
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime:     at com.example.com.rfcomm_test_00.MainActivity$RxRun.run(MainActivity.java:90)
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
logcat

private class RxRun implements Runnable {

    private BluetoothSocket mRFCSocket = null;
    private OutputStream mRFCOS = null;
    private InputStream mRFCIS = null;

    public RxRun() {
        mSPPCtrl = new SPPCtrl(getApplicationContext());
    }

    @Override
    public void run() {

        //initiating connection
        BluetoothSocket rfcSocket = mSPPCtrl.rfcConnect();

        if (rfcSocket.isConnected()) {
            Message msg = mHandler.obtainMessage(1);
            Bundle b = new Bundle();
            b.putString("CONNECTED", "RFC-SOCKET CONNECTED");
            msg.setData(b);
            mHandler.sendMessage(msg);
            //assigning stream variables
            try {
                this.mRFCOS = rfcSocket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                this.mRFCIS = rfcSocket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            Message msg = mHandler.obtainMessage(0);
            Bundle b = new Bundle();
            b.putString("DISCONNECTED", "RFC-SOCKET NOT CONNECTED");
            msg.setData(b);
            mHandler.sendMessage(msg);
        }

        //send a command to RFC-socket
        try {
            this.mRFCOS.write(DIRON.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        //read received response from RFC-socket
        int readBytes;
        try {
            while (this.mRFCSocket.isConnected() && (readBytes = this.mRFCIS.read(new byte[5120])) != -1) {
                Log.d(TAG, CSubTag.bullet("RxRun", "readBytes:" + readBytes));
                //mtvRx.setText("" + readBytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime: FATAL EXCEPTION: Thread-20426
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime: Process: com.example.com.rfcomm_test_00, PID: 11833
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime:     at com.example.com.rfcomm_test_00.MainActivity$RxRun.run(MainActivity.java:90)
11-13 09:55:49.557 11833-12423/com.example.com.rfcomm_test_00 E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)

好的,如果
rfcSocket.isConnected()
为false,则不初始化
this.mRFCOS
,但您可以稍后访问它

您可能应该在测试
rfcSocket.isConnected()
的if语句中移动第3个和第4个try-catch块