Android: NPE: Observable.takeLast

37 Views Asked by At

I set up some observables like so

companion object {
        var isLocationToolActive: BehaviorSubject<Boolean> = BehaviorSubject.createDefault(false)
        var isAoIToolActive: BehaviorSubject<Boolean> = BehaviorSubject.createDefault(false)
        var pointToPosition: BehaviorSubject<GeoPoint> = BehaviorSubject.createDefault(GeoPoint(0.0, 0.0))
}

init {
   pointToPosition.subscribe {  <--- Throws exception on this line
      // some code
   }

}

But when I try to run it it throws an exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'io.reactivex.Observable io.reactivex.Observable.takeLast(long, java.util.concurrent.TimeUnit, io.reactivex.Scheduler, boolean, int)' on a null object reference
    at io.reactivex.Observable.takeLast(Observable.java:11258)
    at <the line of the code above>

What am I doing wrong?

I tried taking it out of the companion object, I tried calling onNext() before the subscribe line, but none of that solved it. I tried if the other two observables have the same issue (the ones with "false") and they do too.

EDIT: Subscribe method of pointToPosition

companion object {
    private var isPTPActive = false
}

init {
    pointToPosition.subscribe {
        isPTPActive = false
    }
}

I have also tried to just put a Log.v() in the subscribe method, this also crashes.

I tried to eliminate as many things as I possibly could, I commented out all code that was calling pointToPosition, I moved it outside of the companion object block, and as suggested I moved the subscribe function outside of init(). Like so:

var locationHelper = LocationBasedHelper(context, resources)
locationHelper.run()
// where run() has the pointToPosition.subscribe { } in it

All of these changes still result in the same error.

EDIT 2: Seems like this error happens on specific devices. It crashes on Samsung Galaxy Tab S2 (SM-T713), but works on Samsung Galaxy Tab S2 (SM-T710). It also works on the newer Galaxy Tab A8.

0

There are 0 best solutions below