Why does checking the code on SharedPreferences.getString () with a default message say "can produce NPE"?

When launching "Code Analysis / Verification" in Android Studio 1.2.2 with the default settings below:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        String value = sharedPref.getString("somekey", "default");
        if (value.equals("default")) {
            Log.d("MainActivity", "value matches default");
        }
    }
}

it gives the following warning in the Probable Errors section:

The invocation method 'value.equals ("default")' on line 16 can throw 'java.lang.NullPointerException'

However, if the preference does not exist, the default value will be returned. Even if the preference value is explicitly set to null with:

sharedPref.edit().putString("somekey", null).commit();

the default value will be returned (presumably under the hood, this actually removes the preference, but it doesn’t matter here).

, NPE, null , :

String value = sharedPref.getString("somekey", null);

, - , , . , null - , , .

, " ", " ", , ", ", "lint " , ( ), - " ".

:

  • - , ?
  • ?
  • ?
  • , ?

4. :

  • , . , , .
  • . , .
  • , "default".equals(value). switch(value){...}
  • if (value != null) {...}. , .

3. :

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String value = null;
        if (value.equals("default")) {
            Log.d("MainActivity", "value matches default");
        }
    }
}

, . .

...

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String value = "test";
        if (value.equals("default")) {
            Log.d("MainActivity", "value matches default");
        }
    }
}

, . .

...

public class MainActivity extends Activity {
    private String getString() {
        String value = null;
        if (new Random().nextBoolean())
            value = "test";
        return value;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String value = getString();
        if (value.equals("default")) {
            Log.d("MainActivity", "value matches default");
        }
    }
}

, (), ! .

, @Nullable getString(). , . SharedPreferences getString() .

, . a @NonNull getString(). :

'' null, , @NotNull ( 28)

, , getString() null, ?

, .

:

, , @Nullable @NotNull, NULL ( -null, ) , . NullPointerException.

, .

, @Nullable. SharedPreferences getString() , . ?

+4
1

- , ?

, . - , .

? ?

, .

, null, unchecked, , .

, ?

"default".equals(value)

, . NPE , .

, , :

//noinspection ConstantConditions    
switch (value) {
    ...
+2

All Articles