Skip to content Skip to sidebar Skip to footer

Null Pointer Exception When Trying To Send Message From One Activity To Another

Why is there NullPointerException error when I try to implement the code which sends a string message from one activity to the BluetoothService Activity which extends Service. Her

Solution 1:

I guess you are getting the exception because you haven't initialized BluetoothService.

privateBluetoothServicemChatService=null;

And you are using mChatService.getState() in your sendMessage() method without initializing.

Solution 2:

Try

if(mChatService == null)
         return;
     if (message.length() > 0 && mChatService.getState() == BluetoothService.STATE_CONNECTED) {
         byte[] send = message.getBytes();
         mChatService.write(send);
     }

Solution 3:

BluetoothServicemChatService=newBluetoothService(args);

You need to assign an instance at some point to the variable.

Post a Comment for "Null Pointer Exception When Trying To Send Message From One Activity To Another"