Search code examples
javachecker-framework

How to circumvent checker framework type.invalid error?


We have a library we've written and after introducing the Checker Framework NullnessChecker to verify its code, it fails to compile (as expected). I've fixed all the obvious errors, but this one I cannot find out how to fix...

Here's the offending function's signature:

private static @Nullable char[] getChars(char ch)

And the call site where the error occurs:

@Nullable char[] replacement = getChars( string.charAt( index ) );

Can anyone tell me how to get checker to accept this? It seems to me to be correct code.

EDIT

The error:

[type.invalid] [@Initialized, @Nullable] may not be applied to the type "@Initialized @Nullable char"

Solution

  • The error message

    [@Initialized, @Nullable] may not be applied to the type "@Initialized @Nullable char"
    

    would have been clearer if it had just been

    @Nullable may not be applied to the type "char"
    

    The issue is that char is a primitive type. It doesn't make sense to speak of @Nullable char or @NonNull char. Nullness is applicable only to object (non-primitive) types. The same is true of initialization.

    If you want to specify a nullable array of char -- that is, the variable replacement is either null or is an array of char -- then write it this way:

    char @Nullable [] replacement = ...;
    

    If you write

     @Nullable char [] replacement = ...;
    

    then that is an array of @Nullable char.

    This is a standard part of the Java type annotation syntax, and is not specific to the Checker Framework. However, there is a FAQ about this in the Checker Framework manual.