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.

Question: how can BehavioralRelay value be nullable?

See original GitHub issue

Version: 2.1.0 RxJava: 2.2.6

Since the default value is always there and not null

/**
     * Constructs a BehaviorRelay with the given initial value.
     * @param defaultValue the initial value, not null (verified)
     * @throws NullPointerException if {@code defaultValue} is null
     */
    BehaviorRelay(T defaultValue) {
        this();
        if (defaultValue == null) throw new NullPointerException("defaultValue == null");
        value.lazySet(defaultValue);
    }

and accept(value) doesn’t accept null either

@Override
    public void accept(T value) {
        if (value == null) throw new NullPointerException("value == null");

        setCurrent(value);
        for (BehaviorDisposable<T> bs : subscribers.get()) {
            bs.emitNext(value, index);
        }
    }

How can the value ever be null? 🙏 🤔

/**
     * Returns a single value the Relay currently has or null if no such value exists.
     * <p>The method is thread-safe.
     */
    @Nullable
    public T getValue() {
        return value.get();
    }

I don’t see anywhere value.lazySet or value.set or value = is used in BehavioralRelay.java where it could cause the value to be missing either Hence couldn’t answer my own question

How I use BehavioralRelay

val myRelay :BehavioralRelay<Int> = BehavioralRelay.createDefault(0)
//...
val latestValue = myRelay.value
// do stuffs with the latest value

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, Jul 22, 2021

Like @oldergod said, if you call create() the value will be null until one is supplied so the annotation is correct.

1reaction
JakeWhartoncommented, Jun 30, 2021

Looks like an accidental holdover from reducing the RxJava BehaviorSubject sources: https://github.com/ReactiveX/RxJava/blob/24df131ecc012aa0591b9123b79a1f0f6a14a4f9/src/main/java/io/reactivex/subjects/BehaviorSubject.java#L320-L327

Feel free to send a PR to change it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Behavior Relay accept Nullable Type in Kotlin #54 - GitHub
Is it possible that BehaviorRelay accept nullable type in kotlin and pass null in accept() or createDefault()?
Read more >
Behaviour subject initial value null? - angular - Stack Overflow
The purpose of BehaviorSubject is to provide initial value. It can be null or anything else. If no valid initial value can be...
Read more >
Null values and the nullable type - IBM
To indicate that a value variable is nullable, append a question mark (?) to the type when you declare it. This works in...
Read more >
Design with nullable reference types - Microsoft Learn
This advanced tutorial provides an introduction to nullable reference types. You'll learn to express your design intent on when reference ...
Read more >
BehaviorRelay (RxRelay 2.0.0 API)
Returns a single value the Relay currently has or null if no such value exists. Object[], getValues(). Returns an Object array containing snapshot...
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