NullPointerException for empty preferences.
See original GitHub issueWith 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:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Exatlcy what I described in #49
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 justrxPref.getString("string", "some default");
and internallynull
check that default can’t be null andthrow
a properly descriptive error message.