0

I'm creating a bluetooth application and I'm trying to send a data to a private method from a public method but i got this error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

the public method:

public void showTimeSet(long interval, Context context){
    if(interval !=0){
        Toast.makeText(context,"Countdown second is "+interval,Toast.LENGTH_LONG).show();
        String msg= String.valueOf(interval);
        try {
            sendMessage(msg);
        }catch (Exception e){
            Log.e(TAG, "showTimeSet Error sending message: "+e);
        }
    }
}

and the sendMessage method:

private void sendMessage(String message) {
    // Check that we're actually connected before trying anything
    if (mChatService.getState() != BluetoothService.STATE_CONNECTED) {
        Toast.makeText(getActivity(), R.string.not_connected, Toast.LENGTH_SHORT).show();
        return;
    }

    // Check that there's actually something to send
    if (message.length() > 0) {
        // Get the message bytes and tell the BluetoothService to write
        byte[] send = message.getBytes();
        mChatService.write(send);

        // Reset out string buffer to zero and clear the edit text field
        mOutStringBuffer.setLength(0);
        mOutEditText.setText(mOutStringBuffer);
    }
}
il_raffa
  • 5,090
  • 129
  • 31
  • 36
user9523333
  • 187
  • 18

1 Answers1

0

It seems the parameter context you passed into showTimeSet, is null, thus resulting in a nullpointer exception when trying to use that context for the Toast.

Check your function call for showTimeSet, and make sure context is an actual Activity/Fragment Context etc. where the toast can be displayed at.

Jujinko
  • 319
  • 3
  • 21