NullPointerException on method called in service if app is killed

204 views Asked by At

I have the following onCreate() in my service

@Override
    public void onCreate() {
        broadcaster = LocalBroadcastManager.getInstance(this);
        map = new HashMap<>();
        beaconFilterModel = new BeaconFilterModel();
        stateList = new ArrayList<>();
        lastUpdateMap = new HashMap<>();
        waitingForExit = new HashMap<>();
        firstFind = false;
        proximityBeaconList = new ArrayList<>();
        previousProximityMap = new HashMap<>();
        mHandler = new Handler();
        KontaktSDK.initialize("MdahnpkIOBvCxfOOmCKHtCdTaEXaqcSx");
        proximityManager = new ProximityManager(this);
        proximityManager.setIBeaconListener(createIBeaconListener());
        proximityManager.filters().iBeaconFilter(new IBeaconFilter() {
            @Override
            public boolean apply(IBeaconDevice iBeaconDevice) {

                return compareMajor(beaconFilter, iBeaconDevice)
                        && compareUUID(beaconFilter, iBeaconDevice)
                        && compareMinor(beaconFilter, iBeaconDevice);
            }
        });
        proximityManager.connect(new OnServiceReadyListener() {
            @Override
            public void onServiceReady() {

                proximityManager.configuration().scanPeriod(ScanPeriod.RANGING);
                proximityManager.startScanning();
            }
        });
    }

and the following onStartCommand()

@Override
public int onStartCommand(Intent intent,int flags, int startId) {
    if (BluetoothAdapter.getDefaultAdapter() == null ||!BluetoothAdapter.getDefaultAdapter().isEnabled()) {
        this.stopSelf();
    }
    // If we get killed, after returning from here, restart
    return START_STICKY;
}

Here is my compareUUID

public boolean compareUUID(BeaconFilterModel beaconFilterModel, IBeaconDevice iBeaconDevice) {
    if(beaconFilterModel.getUuid().isEmpty() || beaconFilterModel.getUuid() == null) {
        return true;
    } else {
        return  UUID.fromString(beaconFilterModel.getUuid()).equals(iBeaconDevice.getProximityUUID());
    }
}

Now, when I app is running, the service works properly. But if I kill my Ap and then restart it, my app crashes with the stack trace:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference
       at com.nicbit.proximitydemo.services.ProximityScanService.compareUUID(ProximityScanService.java:68)
       at com.nicbit.proximitydemo.services.ProximityScanService$1.apply(ProximityScanService.java:110)
       at com.nicbit.proximitydemo.services.ProximityScanService$1.apply(ProximityScanService.java:105)
       at com.kontakt.sdk.android.ble.discovery.ibeacon.IBeaconParser.filter(IBeaconParser.java:100)
       at com.kontakt.sdk.android.ble.discovery.ibeacon.IBeaconDiscoverer.performDiscovery(IBeaconDiscoverer.java:59)
       at com.kontakt.sdk.android.ble.service.DefaultBluetoothDeviceDiscoverer.performDiscovery(DefaultBluetoothDeviceDiscoverer.java:35)
       at com.kontakt.sdk.android.ble.service.MonitorCallback$1.run(MonitorCallback.java:53)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:152)
       at android.app.ActivityThread.main(ActivityThread.java:5507)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

P.S. beaconFilterModel is static

1

There are 1 answers

3
Noorul On

Change the order of condition check:

before use the object first, it has to be check with null object reference.

  if( beaconFilterModel.getUuid() == null||beaconFilterModel.getUuid().isEmpty()){

  }