Carling Knight Carling Knight -4 years ago 221
Android Question

Bluetooth Socket OutputStream NullPointerException when calling Write()

I have a strange issue where apparently my outputstream is null even though I have multiple checks and have used the debugger to confirm it is not null. So the code in question:

protected void onHandleIntent(Intent intent) {
device = (BluetoothDevice) intent.getExtras().get("device");

if (device != null) {
Log.v("DeviceManager", "Beginning attempts to communicate to: " + device.getName());
try {
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
outputStream = socket.getOutputStream();
inputStream = socket.getInputStream();

while (Utils.appIsInForeground && device != null) {
if (outputStream == null){
Log.e("DeviceManager", "Looks like the outputStream is null...");
if (inputStream == null){
Log.e("DeviceManager", "And input stream is null, are you even connected to the device?");
Log.e("DeviceManager", "Although strangely, input stream has been set.");
String myString = "This is a string!";
byte[] myByteArray = myString.getBytes("UTF-8");
//Crashes here:

} catch (IOException e) {
Log.e("DeviceManager", "Can't create a socket to the bluetooth device.");
} catch (InterruptedException e) {
Log.e("DeviceManager", "The sleep thread was interrupted.");
} catch (NullPointerException e) {
Log.e("DeviceManager", "A null pointer exception, this is probably not good.");
}else {
Log.e("DeviceManager", "The Device Manager has been passed a null object instead of a device.");

The stack trace is as follows:

03-23 15:55:12.134 3042-3171/ W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void[], int, int)' on a null object reference
03-23 15:55:12.135 3042-3171/ W/System.err: at android.bluetooth.BluetoothSocket.write(
03-23 15:55:12.136 3042-3171/ W/System.err: at android.bluetooth.BluetoothOutputStream.write(
03-23 15:55:12.136 3042-3171/ W/System.err: at
03-23 15:55:12.137 3042-3171/ W/System.err: at
03-23 15:55:12.138 3042-3171/ W/System.err: at$ServiceHandler.handleMessage(
03-23 15:55:12.138 3042-3171/ W/System.err: at android.os.Handler.dispatchMessage(
03-23 15:55:12.139 3042-3171/ W/System.err: at android.os.Looper.loop(
03-23 15:55:12.139 3042-3171/ W/System.err: at

This what the debugger thinks of each of the variables:

myByteArray = {byte[17]@4799}
this = {DeviceManager@4698}
intent = {Intent@4705} "Intent { (has extras) }"
socket = {BluetoothSocket@4706}
myString = "This is a string!"
myByteArray = {byte[17]@4799}
outputStream = {BluetoothOutputStream@4743}

I'm utterly baffled at this point, nothing is null yet the error persists. I have also tried the following:

outputStream.write(myByteArray, 0, myByteArray.length);

It produces the exact same trace.

Answer Source

You need to call socket.connect() before using input/output stream. See my detailed answer

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download