[英]inputStream.read() causes NullPointerException (after having checked inputStream!=null)
我正在編寫一個需要與藍牙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");
在發現階段之后,我得到了我需要連接的藍牙設備,然后我獲得了套接字,輸入和輸出流。
然后我有一個從輸入流中讀取的線程。
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:為什么讀取(byte [],int,int)如果我試圖調用read()?
我發現了錯誤,你沒事:插座出錯了......我需要調用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:為什么NullPointerException如果我檢查inputStream!= null?
你得到一個NullPointerException,而不是因為你的inputStream是null,你的InputStream很好; 但是在BluetoothSocket.java
或BluetoothInputStream.java
有一個InputStream,它是null,並且正在調用read方法。
問題2:為什么讀取(byte [],int,int)如果我試圖調用read()?
涉及問題一。 int java.io.InputStream.read(byte[], int, int)
在其他一個文件中調用,在null InputStream上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.