Android read()导致NullPointerException(在选中inputStream!=null之后)
我正在编写一个需要与Bluetooth 2.1设备交换数据的应用程序。 我已经做了好几次了,但这次发生了一些奇怪的事情Android read()导致NullPointerException(在选中inputStream!=null之后),android,bluetooth,nullpointerexception,inputstream,Android,Bluetooth,Nullpointerexception,Inputstream,我正在编写一个需要与Bluetooth 2.1设备交换数据的应用程序。 我已经做了好几次了,但这次发生了一些奇怪的事情 Log.d("TAG", "connectToDevice"); if(macAddress != null) deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress); Log.d("TAG", "macAddress != null"); if(de
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");
在发现阶段之后,我得到了我需要连接的Bluetooth设备,然后我获得了套接字、输入和输出流
然后我有一个线程从输入流读取数据
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:如果我正在检查inputStream,为什么会出现NullPointerException!=空的
问题2:如果我试图调用read(),为什么要读取(字节[],int,int)?问题1:如果我正在检查inputStream,为什么要读取NullPointerException!=空? 您得到了一个NullPointerException,并不是因为您的inputStream为null,而是因为您的inputStream很好;但是,在
BluetoothSocket.java
或BluetoothInputStream.java
中有一个InputStream,它是空的,并且正在对其调用读取方法
问题2:如果我试图调用read(),为什么要读取(字节[],int,int)?
与问题一有关
int java.io.InputStream.read(byte[],int,int)
正在您的另一个文件中以空InputStream调用。我发现了错误,您没有问题:套接字有错误。。我需要调用socket.connect()
这就是为什么inputStream没有准备好,我得到了那个错误
对不起,我只是没看到。。希望它能帮助别人 问题2:因为read()在内部调用read(byte[],int,int)。注意android框架中调用问题1上面的两个步骤:这不是在代码中抛出异常,而是在框架中抛出异常。所以不是你的输入流是空的,而是Android试图调用的输入流中的输入流。因此,空值表示框架错误或蓝牙套接字中的某些内容处于错误状态。连接的第152行是什么?@Husman:152行是inputStream.read()@Gabe:Ok这也是我的猜测,但你认为它可能与套接字或我正在使用的安卓API有关吗?你在哪里初始化inputStream?它可能不为null,但可能未处于ready to read()状态,或者底层流可能未就绪。好的,这是我的猜测,但感谢您向我确认。原因是什么?我能想象的唯一原因是与插座有关的东西。。
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();
}
}