Shiv
Shiv

Reputation: 191

Nullpointer exception while reading bluetooth device

In my app I am scanning bluetooth device. While reading I am getting Nullpointer Exception, below is the code snippet.

    private  BluetoothAdapter.LeScanCallback mLeScanCallback =
                new BluetoothAdapter.LeScanCallback() {
                    @Override
                    public void onLeScan(final BluetoothDevice device, int rssi,
                             byte[] scanRecord) {
                        // TODO Auto-generated method stub

                         getActivity().runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub              

  1.                            mLeDeviceListAdapter.addDevices(device);
  2.                              mLeDeviceListAdapter.notifyDataSetChanged();

                            }
                        });
                      }
                     }

I am getting NullpointerException in above line number 1. Here mLeDeviceListAdapter is a object of Custome list adapter.

public class LeDeviceListAdapter extends BaseAdapter{

        private ArrayList<BluetoothDevice> mLeDevices;
        private LayoutInflater mInflator;
        Bundle savedInstanceState;

        public LeDeviceListAdapter() {
            // TODO Auto-generated constructor stub
            super();                
            mLeDevices = new ArrayList<BluetoothDevice>();
            mInflator = AvailableDevices.this.getLayoutInflater(savedInstanceState);    

        }

        public void addDevices(BluetoothDevice device){

            if(!mLeDevices.contains(device)){
                mLeDevices.add(device);             

            }

        }

In the OnActivityCreated method I am calling all above functions like below.

public class AvailableDevices extends ListFragment {

   public void onActivityCreated(Bundle savedInstanceState) {

         LeDeviceListAdapter dListAdapter =  new LeDeviceListAdapter();     

        setListAdapter(dListAdapter);
        scanLeDevice(true);
  }

The scanLeDevice function will call mLeScanCallback method.

Where I am missing to get NullpointerException..

Thanks

Upvotes: 0

Views: 116

Answers (1)

Rustam
Rustam

Reputation: 6515

Initialize your mLeDeviceListAdapter

mLeDeviceListAdapter=new LeDeviceListAdapter();

Upvotes: 1

Related Questions