question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullPointerException for empty preferences.

See original GitHub issue

With the change on Rx2 that null cannot be emitted, the simples code implementation like below crashes with NPE (stack below).

As far as I could find, it should be easy to fix. The call that triggers a initial load on RealPreferences@31 (.startWith("<init>")) should only happen if(preferences.containsKey(key)

        RxSharedPreferences rxPref = RxSharedPreferences.create(getSharedPreferences("myprefs", 0));
        Preference<String> rxString = rxPref.getString("string");
        rxString.asObservable().subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {

            }
        });
java.lang.NullPointerException: The mapper function returned a null value.
at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)
at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:58)
at io.reactivex.internal.operators.observable.ObservableConcatMap$ConcatMapDelayErrorObserver.drain(ObservableConcatMap.java:462)
at io.reactivex.internal.operators.observable.ObservableConcatMap$ConcatMapDelayErrorObserver.onSubscribe(ObservableConcatMap.java:325)
at io.reactivex.internal.operators.observable.ObservableFromArray.subscribeActual(ObservableFromArray.java:29)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at io.reactivex.internal.operators.observable.ObservableConcatMap.subscribeActual(ObservableConcatMap.java:54)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:32)
at io.reactivex.Observable.subscribe(Observable.java:10514)
at io.reactivex.Observable.subscribe(Observable.java:10500)
at io.reactivex.Observable.subscribe(Observable.java:10403)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
PaulWoitaschekcommented, Jan 26, 2017

Exatlcy what I described in #49

Well then you should catch that error and throw an appropriate one. I think it’s appropriate to make the default value strictly NonNull and throw an NPE when the default value is null. Thats consistent to RxJava 2. If people do not have a default value they should use an Optional.

1reaction
budiuscommented, Jan 20, 2017

The code snippet I showed is a simplified version of my code. My code in reality is from a custom object that is created from a GsonPreferenceAdapter, that come from a builder. For my current workaround I’ve added a default empty object (as you suggest) and check/filter if(object.apiKey == null).

I understand and agree that might not be the right solution, as it doesn’t fix the case when u clear the preference value/have a null value.

But still, a straight forward crash like this is not the best behavior, and there should be something that can be done to improve this great library =)

So I’ll suggest to maybe remove the rxPref.getString("string"); method and leave just rxPref.getString("string", "some default"); and internally null check that default can’t be null and throw a properly descriptive error message.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix and Avoid NullPointerException in Java - Rollbar
To fix the NullPointerException in the above example, the string should be checked for null or empty values before it is used any...
Read more >
Shared Preferences Null Pointer Exception - java
I am working on adding Shared Preferences to my Note taking Android App and I am running into a peculiar null pointer exception....
Read more >
Java NullPointerException - Detect, Fix, and Best Practices
NullPointerException when calling an instance method​​ We are getting NullPointerException in the statement t. foo("Hi"); because “t” is null ...
Read more >
Null pointer exception to fetch custom settings data
I have below code where I face null pointer exception when the custom settings data is not populated. I simply need ...
Read more >
How to resolve the java.lang.NullPointerException - Educative.io
In Java, the java.lang.NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found