3

I'm using annotations library supported by Android. It is useful for checking NullpointerExceptions. But, it also annoying if I checked the variable if it is null.

In this case:

@Nullable String value; boolean isValid(String target) { return (target != null && target.length() > 0); } void test() { if (isValid(value)) { Log.d("TEST", value); // Do something. } } 

In above code, I already checked if 'value' is null, but still got the warning of "It maybe produce NullPointerException".

Is there some annotations that I checked null-type in this function?

3
  • 1
    u can use assert target != null above the return statement Commented Nov 17, 2017 at 6:31
  • 1
    Why not initialize the String value to an empty string in the constructor or at declaration itself? Commented Nov 17, 2017 at 6:31
  • add @SuppressWarnings Commented Nov 17, 2017 at 6:47

2 Answers 2

2

You can use the annotation:

@SuppressWarnings("ConstantConditions") 
Sign up to request clarification or add additional context in comments.

Comments

1

In android studio Go to

file-->other settings---> default settings (defaultpreference)--->inspections--->java---> @notnull and nullable Probelms

uncheck it and click ok.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.