score:2

Accepted answer

i think if you refactored a bit to use optional you'd be better off.

@nonnull optional<string> optnullorempty(@nullable string s) {
  if (s == null || s.length() == 0)
    return optional.empty();
  else
    return optional.of(s);
}

then you can use it as:

void test(@nullable string s) {
  @nonnull optional<string> os = optnullorempty(s);
  if (os.ispresent()) {
    @nonnull string s1 = os.get();
    s1.length(); // no warning :-)
  }
}

score:3

is there a reason for you not to use if (o != null) ?

the purpose of objects.isnull is to be used as a predicate : .filter(objects::isnull)

this warning might be a bug though. you can annotate your method with @suppresswarnings if you really want it to go away


Related Query

More Query from same tag