inputStream.read()导致NullPointerException(在检查inputStream之后!= null)

时间:2014-06-17 15:23:49

标签: android bluetooth nullpointerexception inputstream

我正在编写一个需要与蓝牙2.1设备交换数据的应用程序。 我已经做了好几次了,但这次发生了一些奇怪的事情。

    Log.d("TAG", "connectToDevice");

    if(macAddress != null)
        deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress);

    Log.d("TAG", "macAddress != null");


    if(deviceToConnect != null)
        try {
            btSocket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(SharedIncludes.SPP_UUID));
        } catch (IOException e) {
            btSocket = null;
            e.printStackTrace();
        }

    Log.d("TAG", "deviceToConnect != null");

    if(btSocket != null){

        try {
            inputStream = btSocket.getInputStream();
            Log.d("TAG", "inputStream OK");

        } catch (IOException e) {
            inputStream = null;
            Log.d("TAG", "inputStream KO");
            e.printStackTrace();
        }

        try {
            outputStream =  btSocket.getOutputStream();
            Log.d("TAG", "outputStream OK");

        } catch (IOException e) {
            outputStream = null;
            Log.d("TAG", "outputStream KO");
            e.printStackTrace();
        }

    }


    Log.d("TAG", "btSocket != null");
    Log.d("TAG", "onConnectionEstablished");

在发现阶段之后,我得到了我需要连接的BluetoothDevice,然后我获得了socket,input&输出流。

然后我有一个从输入流中读取的线程。

   int byteRead;

    while (listening) {
        try {
            if(inputStream!=null){
                Log.d("TAG", "inputStream: " +inputStream);
                byteRead = inputStream.read();
            }else{
                Log.d("TAG", "inputStream is null!");
                continue;
            } // Rest of the code here

执行inputStream.read()行时出现此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[], int, int)' on a null object reference

at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:427)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:60)
at com.me.testapplication.Connection_BT21.run(Connection_BT21.java:152)

问题1:为什么NullPointerException如果我正在检查inputStream!= null?

问题2:如果我试图调用read(),为什么要读取(byte [],int,int)?

2 个答案:

答案 0 :(得分:7)

我发现错误,你没事:套接字出错了......我需要调用socket.connect()!

if(macAddress != null)
    deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress);

Log.d("TAG", "macAddress != null");


if(deviceToConnect != null)
    try {
        btSocket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(SharedIncludes.SPP_UUID));
    } catch (IOException e) {
        btSocket = null;
        e.printStackTrace();
    }

Log.d("TAG", "deviceToConnect != null");

if(btSocket != null){
 //This was the line missing!
 btSocket.connect();

    try {
        inputStream = btSocket.getInputStream();
        Log.d("TAG", "inputStream OK");

    } catch (IOException e) {
        inputStream = null;
        Log.d("TAG", "inputStream KO");
        e.printStackTrace();
    }

    try {
        outputStream =  btSocket.getOutputStream();
        Log.d("TAG", "outputStream OK");

    } catch (IOException e) {
        outputStream = null;
        Log.d("TAG", "outputStream KO");
        e.printStackTrace();
    }

}

这就是为什么inputStream没有准备就绪,我得到了那个错误。

抱歉,我只是没有看到它..希望它可以帮助别人!

答案 1 :(得分:0)

问题1:为什么NullPointerException如果我正在检查inputStream!= null?

你得到一个NullPointerException,不是因为你的inputStream是null,你的InputStream很好;但是在BluetoothSocket.javaBluetoothInputStream.java中有一个InputStream,它是null,并且正在调用read方法。

问题2:如果我试图调用read(),为什么要读取(byte [],int,int)?

涉及问题一。 {null}正在其他一个文件中调用int java.io.InputStream.read(byte[], int, int),在null InputStream上。