简体   繁体   中英

How to tell Android Studio that a variable may become null?

I have the following code, where the second "Callback" parameter MUST be supplied by the caller (the reason why I have annotated it with @Nonnull).

public static void load(@Nonnull final String channelId, @Nonnull final Callback callback) {
    Observable.create(new Observable.OnSubscribe<Channel>() {
        @Override public void call(Subscriber<? super Channel> subscriber) {
            try {
                Channel channel = new Channel().setApiChannel(MobileService.api().channels().get(channelId).execute(), UpdateFields.ALL);
                subscriber.onNext(channel);
            } catch (Exception e) {
                subscriber.onError(e);
            }
            subscriber.onCompleted();
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()). subscribe(new Subscriber<Channel>() {
        @Override public void onCompleted() {
        }
        @Override public void onError(Throwable t) {
            log.warning("could not load channelId = " + channelId + ", throwable = " + t.getMessage());
            if (callback != null) { callback.onError(channelId, t); }
        }
        @Override public void onNext(Channel channel) {
            if (callback != null) { callback.onSuccess(channel); }
        }
    });
}

The Callback interface looks like this:

public interface Callback {
    void onSuccess(Channel channel);
    void onError(String channelId, Throwable throwable);
}

To call back the caller, I make a null check like this

if (callback != null) { callback.onSuccess(channel); }

because the caller could become null any time while my observable does its background job. For example, the caller might be a view which could be paused/destroyed/garbage collected [Android does funny things sometimes], in which case my code would fail without the null check.

But Android Studio does not recognize that possibility and displays a Lint warning, which it doesn't allow me to suppress either.

打回来

It seems Android Studio does not realize the variable could become null by other causes except my own function. Well, I even admit it should not as it is final, and I was very surprised when that line initially failed (I didn't have the null check at that point because I believed it could never become null).

Question: How can I tell Android Studio to suppress the warning, despite the parameter being annotated as @Nonnull final?

And then: If somebody could explain how the final variable can become null at any time when the underlying reference becomes null. My concept of final is the variable is "cloned" and kept with the context, which is obviously not happening here. Is final "forward only" - meaning my own code cannot change that variable anymore, but the caller still can?

I think the variable could not get null, since you are passing Callback reference and not WeakReference. still if the Callback is getting executed on a separate service process then passing a View as Callback would be bad practice. on the final note you can force remove lint with @SuppressLint()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM