为什么来自BluetoothSocket的输入/输出流被评估为“非空",然后抛出空指针异常? [英] Why do input/outputstream from BluetoothSocket get evaluated as NOT null, then throw null pointer exception?

查看:66
本文介绍了为什么来自BluetoothSocket的输入/输出流被评估为“非空",然后抛出空指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从蓝牙套接字创建的输入流和输出流,在检查套接字是否为空(这全部在OnCreate函数中)之后,我尝试向其中写入一些内容:

I have an inputstream and outputstream created from a bluetooth socket that I try to write something to after checking if the socket is null (this is all within the OnCreate function):

BluetoothDevice btDevice = ...//get bluetooth device from user's selection of paired devices

UUID MY_UUID = btDevice.getUuid();

BluetoothDevice remotedevice = btAdapter.getRemoteDevice(btDevice.getAddress());

BluetoothSocket btsocket = remotedevice.createRfcommSocketToServiceRecord(MY_UUID);

InputStream inputStream = btsocket.getInputStream();
OutputStream outputStream = btsocket.getOutputStream();

if (outputStream != null){
         outputStream.write(1);
}

无论是否连接了蓝牙设备或蓝牙设备在范围内,输出流都将被评估为NOT null并将尝试对其进行写入.这种写尝试是触发空指针异常的原因.

Whether or not the bluetooth device is connected or in range, the output stream will be evaluated as NOT null and the attempt will be made to write to it. This write attempt is what triggers the null pointer exception.

为什么会这样?为什么outputStream的评估结果在一行中不为null,然后在下一行立即引发null指针异常?我已经尝试使用几种不同的配对蓝牙设备来获得相同的结果.

Why does this happen? Why is the outputStream evaluated NOT null on one line, and then throws a null pointer exception immediately on the next line? I've tried this with a few different paired bluetooth devices and get the same result.

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

推荐答案

OutputStream outputStream = btsocket.getOutputStream();

outputStream 永远不会为null,因此您的null检查将始终返回true.

outputStream is never null, so your null check will always return true.

OutputStream getOutputStream ()
Get the output stream associated with this socket.

The output stream will be returned even if the socket is not yet connected, but operations on that stream will throw IOException until the associated socket is connected.

理想情况下,根据文档,它应该抛出 IOException

Ideally as per documentation it should throw IOException (and it does so for API LEVEL >= 21)

public void write(int oneByte) throws IOException {
    byte b[] = new byte[1];
    b[0] = (byte)oneByte;
    mSocket.write(b, 0, 1);
}

mSocket.write(b,0,1)使用 mSocketOS null 并导致异常.使用API​​> = 21,您将得到 IOException 并显示消息在空OutputStream上调用写入"

mSocket.write(b, 0, 1) uses mSocketOS which is null and causing the exception. With API >= 21 you will get IOException with message "write is called on null OutputStream"

您可以使用 btsocket.connect()

You can use btsocket.connect() to initiate the outgoing connection which will initialize required mSocketOS.

在写入套接字之前,您应该调用 isConnected(),仅当与远程设备的活动连接有效时,它才会返回true.

Before writing to socket you should call isConnected() which will return true only if there is an active connection with remote device.

 if(btsocket.isConnected()) {
     outputStream.write(1);
 }

这篇关于为什么来自BluetoothSocket的输入/输出流被评估为“非空",然后抛出空指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆