Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inputStream.read() causes NullPointerException (after having checked inputStream!=null)

I am writing an application that needs to exchange data with a Bluetooth 2.1 device. I have done it several times but this time something weird is happening.

    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");

After the discovering phase I get the BluetoothDevice I need to connect to and then I obtain the socket, input & output streams.

I then have a thread that reads from the input stream.

   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

I get this error while executing the inputStream.read() line:

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)

Problem 1: why NullPointerException if I am checking that inputStream != null?

Problem 2: why read(byte[], int, int) if I am trying to call read()?

like image 403
The Good Giant Avatar asked Jun 17 '14 15:06

The Good Giant


1 Answers

I found the error, you were all right: there was an error with the socket.. I needed to call 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();
    }

}

That was why the inputStream was not ready and I got that error.

Sorry, I just didn't see it.. Hope it can help someone!

like image 164
The Good Giant Avatar answered Sep 21 '22 01:09

The Good Giant