Chuchaki
Chuchaki

Reputation: 371

Attempt to invoke interface method '...' on a null object reference

I'm working on an App with Bluetooth functionalities. I use a fragment to scan for and list bluetooth device. On click there is a callback to the main activity providing the selected bluetooth device.

I started with a Smartphone with Android 6 (API 23) and then had to adapt the code for the use with Android 5.0 (API 21).
I just changed the minSDK to API21 and rebuilt the project without any problems.

The App works without any problems on the smartphone. The Tablet with Android 5 runs the app but crashes with a null pointer exception when I select a bluetooth device.

I have not found any solution to this problem and dont know how to proceed. Maybe someone can help? :-)

The Log is:

me: FATAL EXCEPTION: main
Process: de.tuhh.et5.tills.biocontrol, PID: 26512
java.lang.NullPointerException: Attempt to invoke interface method 'void de.tuhh.et5.tills.biocontrol.activity.BLEListFragment$OnBLEDeviceSelectedListener.OnBLEDeviceSelected(android.bluetooth.BluetoothDevice)' on a null object reference                                                                                    at de.tuhh.et5.tills.biocontrol.activity.BLEListFragment.onListItemClick(BLEListFragment.java:92)
at android.app.ListFragment$2.onItemClick(ListFragment.java:160)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1185)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3222)
at android.widget.AbsListView$3.run(AbsListView.java:4138)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5568)
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:955)                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

Since it is a lot of code I'll try to summarize the important pieces of code:

The error refers to this method

@Override
public void onListItemClick(ListView mBluetoothLeDeviceList, View v, int position, long id) {
    if(DEBUG)d("onListItemClick()");
    mBluetoothLeDeviceList.getChildAt(position).setBackgroundColor(Color.GREEN); // set background
    mBluetoothLeDeviceList.getChildAt(position).setFocusable(false); // not clickable again
    mCallback.OnBLEDeviceSelected(mListAdapter.getDevice(position));
}

The last line with the mCallback... generates the Null Pointer Exception. The Bluetooth device is definetly not null, so there must be a problem with the callback that just appears under android 5.0 (doesnt sound right to me :-) )

The callback is created:

OnBLEDeviceSelectedListener mCallback;

and the interface

public interface OnBLEDeviceSelectedListener {
    void OnBLEDeviceSelected(BluetoothDevice device);
}

and this makes sure the listener is implemented in the main activity:

try {
        mCallback = (OnBLEDeviceSelectedListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString()
                + " must implement OnBLEDeviceeSelectedListener");
    }

The main activity implements BLEListFragment.OnBLEDeviceSelectedListener and contains

@Override
public void OnBLEDeviceSelected(BluetoothDevice device) {
.
.
.}

Thats about it. I find it very weird, that it works on one device and just crashes on the other one without any compiling errors.

I appreciate any idea or hints.

Thanks & Greetings

Upvotes: 7

Views: 16695

Answers (2)

Implement these methods in the fragment its very important.

 @Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
   
    if (context instanceof mCallback) {
        mCallback = (OnBLEDeviceSelectedListener) context;
    }else{
        throw new ClassCastException(context.toString()
            + " must implement OnBLEDeviceeSelectedListener");

    // or you can throw runtime exception like this
    //  throw new RuntimeException(context.toString() + " must 
   //implement OnBLEDeviceeSelectedListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mCallback =null;
}

Upvotes: 0

Vilas
Vilas

Reputation: 1705

Make sure you implement both methods in fragment like this.

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    //Your callback initialization here
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(context);
    //Your callback initialization here
}

Upvotes: 7

Related Questions