0

I am trying to find the right combination of nullness annotations and nullness checking functions in my project. I deal with a lot of String variables that have the possibility of being null, and need to check in a lot of places whether these variables are blank or not. A situation like below occurs frequently...

    private void someMethod(SomeObject obj) {
        @Nullable String nullableStr = obj.getStrThatCouldBeNull();
        if (StringUtils.isNotBlank(nullableStr)) {
            doSomething(nullableStr);
        }
    }

The only issue is that the CharSequence argument to StringUtils.isNotBlank is not annotated with any @Nullable annotations, so the java compiler gives me a warning like....

[argument.type.incompatible] incompatible types in argument.
  found   : @Initialized @Nullable CharSequence
  required: @Initialized @NonNull CharSequence

How can I achieve my desired outcome: nullness annotations I can use for string with a nullness/blank-string check, and also not get warnings from the compiler? The above code works if I annotate the method with @SupressWarning("argument.type.incompatible"), but I would prefer to not have to add this annotation to every method where this happens, which is a fair amount in the project.

Casey Daly
  • 383
  • 8
  • 25
  • What is the desired outcome here? When you get `obj.getStrThatCouldBeNull()` what would you like to happen if the string is null. – Sam Orozco Aug 25 '21 at 22:28
  • If that was null, the if statement would be skipped. The code runs properly if the compiler warnings are suppressed, but I want to get rid of the compiler warnings altogether without suppressing them – Casey Daly Aug 26 '21 at 00:36

0 Answers0