cycun cycun - 1 month ago 13
Android Question

Android-Bluetooth. Null pointer

I am beginner in Objective-programming and I don't know what I should do. I am getting some errors with null pointer in openBT method. Below is my logcat and the code.

MainActivity.java:

public class MainActivity extends ActionBarActivity implements DrawingViewInterface {

private DrawingView drawView;
private ImageButton currPaint;


BluetoothAdapter mBluetoothAdapter;
BluetoothSocket mmSocket;
BluetoothDevice mmDevice;
OutputStream mmOutputStream;
InputStream mmInputStream;
Thread workerThread;
byte[] readBuffer;
int readBufferPosition;
int counter;
volatile boolean stopWorker;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

drawView = (DrawingView)findViewById(R.id.drawing);

drawView.setViewListener(this);



}



public void findBT(View view){

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if(mBluetoothAdapter==null){
Toast.makeText(getApplicationContext(),"Nie wykryto BT", Toast.LENGTH_LONG).show();
}

if(!mBluetoothAdapter.isEnabled()){
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth,0);
}

Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("MattsBlueTooth"))
{
mmDevice = device;
break;
}
}
}

Toast.makeText(getApplicationContext(),"Znaleziono BT", Toast.LENGTH_LONG).show();

}



public void openBT(View view) throws IOException
{
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();


Toast.makeText(getApplicationContext(),"Bluetooth jest otwarty", Toast.LENGTH_LONG).show();

sendData();


}

void sendData() throws IOException
{
String msg = "chuj";
msg += "\n";
mmOutputStream.write(msg.getBytes());
Toast.makeText(getApplicationContext(),"Dane zostały przesłane", Toast.LENGTH_LONG).show();
}


}


Logcat:

03-31 01:00:13.795 18767-18767/com.example.aplikacja3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.aplikacja3, PID: 18767
java.lang.IllegalStateException: Could not execute method of the activity
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)

            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothSocket android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord(java.util.UUID)' on a null object reference
at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:157)
            at java.lang.reflect.Method.invoke(Native Method)
            
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-31 01:35:21.665 20598-20598/com.example.aplikacja3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.aplikacja3, PID: 20598
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4012)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)

            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.BluetoothSocket android.bluetooth.BluetoothDevice.createRfcommSocketToServiceRecord(java.util.UUID)' on a null object reference
at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:157)
            at java.lang.reflect.Method.invoke(Native Method)
            
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)


Anyone can help?

EDIT:

Thank you! You help me. But my problem was different. I forget change name of my device in "FindBT". Ehh..

But now I have other problem. I try connect with my computer but when I try send something I get this:

java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4012)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
Caused by: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:517)
at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:528)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
at com.example.aplikacja3.MainActivity.openBT(MainActivity.java:160)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4761)
            at android.view.View$PerformClick.run(View.java:19767)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5312)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

Answer

In accordace with your logcat, your are calling the method createRfcommSocketToServiceRecord from a null object, that is the mmDevice. The solution is call the openBT only after mmDevice to have a non null value. To avoid the crash, you can put this if condition, as the first line of your openBT method:

if (mmDevice != null) {
    // rest of your method here
Comments