Chuchaki Chuchaki - 3 months ago 6x
Android Question

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:

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(
at android.widget.AdapterView.performItemClick(
at android.widget.AbsListView.performItemClick(
at android.widget.AbsListView$
at android.widget.AbsListView$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(
at$ at

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

The error refers to this method

public void onListItemClick(ListView mBluetoothLeDeviceList, View v, int position, long id) {
mBluetoothLeDeviceList.getChildAt(position).setBackgroundColor(Color.GREEN); // set background
mBluetoothLeDeviceList.getChildAt(position).setFocusable(false); // not clickable again

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

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

Make sure you implement both methods in fragment like this.

public void onAttach(Context context) {
    //Your callback initialization here

public void onAttach(Activity activity) {
    //Your callback initialization here