Null comparison always yields false is false

I’m trying to work on the Chromecast binding a bit, and while this isn’t neither new nor a showstopper, it just annoys me to have a lot of warnings everywhere, because they drown out the “real” warnings that I might need to see.

I have picked one specific example here now, but it’s just an example - the same or a similar issue is found many places in the code.

This is my “example code”:

                MediaStatusResponse mediaStatusResponse = event.getData(MediaStatusResponse.class);
                if (mediaStatusResponse == null) {
                    statusUpdater.updateMediaStatus(null);
                } else {
                    List<MediaStatus> mediaStatuses = mediaStatusResponse.getStatuses();
                    for (MediaStatus mediaStatus : mediaStatuses) {
                        statusUpdater.updateMediaStatus(mediaStatus);
                    }
                }

event.getData() is defined as such:

	/**
	 * Tries to cast the event data to the specified type and return it.
	 *
	 * @param <U> the event data type.
	 * @param cls the {@link Class} of the event data type.
	 * @return The event data cast to the specified type or {@code null}.
	 */
	@Nullable
	<U> U getData(Class<U> cls);

There should be no doubt that it can return null.

However, this is what Eclipse says:

The view of the JavaDoc for the method reveals the extent of the confusion:

It claims that the method is both @NonNull and @Nullable at the same time.

I’ve tried all kind of creative ways to apply @NonNullByDefault({}) to no avail. It is simply adamant that the method is @NonNull when it in fact is @Nullable. I’ve even tried removing @NonNullByDefault from the class, which usually resolves all “null weirdness”, but in this case, it makes no difference.

I just can’t fathom from where it gets the idea that the method is @NonNull - and I can’t find a way to tell it that it’s wrong.

I’ve used Eclipse a lot, and I’ve never seen this issue before, so I’m assuming that this is caused by something in the way OH is configured. I’ve noticed that lastnpe is in use by OH in some way, although I have no idea exactly how. Could this be the cause of the confusion?

Is there anything I can do to get rid of these false warnings - because they end up making the real warnings very hard to spot. I usually don’t “leave” a class until the entire file is without warnings, and I look for warning icons in Package Explorer to quickly find the culprits. But, when everything is full of these false warnings, my “usual process” crash and burn.

I obviously don’t want to use @SuppressWarnings("null"), since it will hide real problems as well.

Even worse, I also get a “unused code” warning here because of the erroneous conclusion it draws about nullness.

Have no clue. There are some edge cases. Did you try to put @Nullable on the same line? Have seen some strange parsing issues in the past

Yes - I tried all the “usual tricks”, nothing changed.

It seems clear to me that for some reason, the “nullness” of the (library) methods are misinterpreted. They are annotated, but with a different annotation (FindBugs), still it shouldn’t be too hard for the Eclipse annotation to be able to interpret them - or at least treat them as if they were unannotated. But, it’s adamant here that the methods are the opposite of what they actually are… and there must be some reason for this.

@J-N-K is an expert on this matter

1 Like

I’m thinking that maybe something can be done using Eclipse External Annotations (EEA) or similar here, but I don’t know where to start looking.

Since I happen to control the external library, I could also do something there, but I don’t want to switch to the Eclipse JDT annotations, as I find the FindBugs (Java 5) ones to be much less intrusive and annoying, and they contain many other useful annotations not related to nullness, like the ones for concurrency. Sure, they don’t cover absolutely all (nullness) corner cases, but they are still a very useful tool and a standardized way of documenting nullness. The amount of workarounds required with the Eclipse JDT annotations are just not worth it IMO. But, if I could somehow embed EEAs with the library, I would gladly do so.

This is quite annoying. I just met another case of this, and I can’t find anything I can do to suppress it except to suppress null checking altogether (plus suppressing dead code evaluation).

It now claims that WeakReference.get() cannot be null. That is insanity, the whole reason WeakReference exists is that it can return null (if the object has been GC’ed). As a result, it claims that all my code that deals with if the object has been GC’ed is designated “Dead Code” and all made yellow, making it impossible to see if there are actual issues with the code.

@J-N-K Any hints of how to resolve this would be appreciated.