Null type mismatch (type annotations) help

Hello,
I get this error during my compilation

 Null type mismatch (type annotations): required 'byte @org.eclipse.jdt.annotation.NonNull[]' but this expression has type 'byte @org.eclipse.jdt.annotation.Nullable[]'

Here is the function

private byte @Nullable [] coverArt = null;

public @Nullable RawType getCoverArt() {
        if (coverArt == null) {
            return null;
        } else {
            return new RawType(coverArt, "image/jpeg");
        }
    }

how can I solve this error?

Hi,
you need first to assign the coverArt variable to a local variable, like in the following sample from the documentation:

private void myFunction() {
    final MyType myField = this.myField; // You need a local copy of the field for thread safety.
    if (myField != null) {
        myField.soSomething();
    }
}

Coding Guidelines - Null annotations

Try

    private byte @Nullable [] coverArt = null;

    public @Nullable RawType getCoverArt() {
        byte[] localCoverArt = coverArt;
        return localCoverArt == null ? null : new RawType(localCoverArt, "image/jpeg");
    }

@jossuar You beat me to it!

1 Like