Fran M.
Fran M.

Reputation: 79

Why may setAdapter() on my ListView produce a NullPointerException?

I'm confused about this situation. The code below works fine, but Android Studio alerts me that setAdapter() may produce 'java.lang.NullPointerException':

public class PlayerManagement extends AppCompatActivity {
    private Cursor c;
    private SQLiteDatabase db;
    private String selectedPlayerId, selectedPlayerName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_management);
        ListView listView = (ListView) findViewById(android.R.id.list);

        try {
            MTGPS_DBHelper dbHelper = new MTGPS_DBHelper(this);
            db = dbHelper.getWritableDatabase();
            new getPlayersCursorAsync().execute();

            final SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(PlayerManagement.this, android.R.layout.simple_list_item_1, c,
                                       new String[]{MTGPS_DB_Contract.Player.COLUMN_NAME_PLAYER_NAME},
                                       new int[]{android.R.id.text1}, 0);
        listView.setAdapter(mAdapter);

...

And this is the XML file activity_player_management:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_players_found"
        android:textSize="25sp" />

</LinearLayout>

As I say, everything works fine, but I don't get why Android Studio alerts me. If I declare the ListView as a class field, then the "problem" disappears. Only when I declare the Listview inside the onCreate method is when I get the alert.

Upvotes: 0

Views: 158

Answers (5)

PEHLAJ
PEHLAJ

Reputation: 10126

Change listView id android:id="@android:id/list" to android:id="@+id/list"

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Upvotes: 0

Pravin Fofariya
Pravin Fofariya

Reputation: 346

Try this,

public class PlayerManagement extends AppCompatActivity {
    private Cursor c;
    private SQLiteDatabase db;
    private String selectedPlayerId, selectedPlayerName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_management);
        ListView listView = (ListView) findViewById(android.R.id.list);

        try {
            MTGPS_DBHelper dbHelper = new MTGPS_DBHelper(this);
            db = dbHelper.getWritableDatabase();
            new getPlayersCursorAsync().execute();

            final SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(PlayerManagement.this, android.R.layout.simple_list_item_1, c,
                                       new String[]{MTGPS_DB_Contract.Player.COLUMN_NAME_PLAYER_NAME},
                                       new int[]{android.R.id.text1}, 0);
        if(mAdapter!=null)
        {
            listView.setAdapter(mAdapter);
        }   
...

Upvotes: 0

Seenu69
Seenu69

Reputation: 1054

Change the code to

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_players_found"
        android:textSize="25sp" />

</LinearLayout>

And in the activity declare them as follows:

ListView listView = (ListView) findViewById(R.id.list);
TextView textView = (TextView) findViewById(R.id.empty);

Upvotes: 0

Sharath kumar
Sharath kumar

Reputation: 4132

Change android:id="@android:id/list" to android:id="@+id/list"

@+id-This is used when you set your own id to a listView in the namespace of your application.

@android:id-This is used when you set id of a view to predefined android namespace.

Also replace android.R.id.list by R.id.list

Upvotes: 0

laalto
laalto

Reputation: 152817

setAdapter() may produce 'java.lang.NullPointerException

That's just a warning. findViewById() returns @Nullable i.e. potentially null and you're calling a method on the return value without a nullity check.

You can suppress the warning e.g. by adding

//noinspection ConstantConditions

on the previous line.

If I declare the ListView as a class field, then the "problem" disappears. Only when I declare the Listview inside the onCreate method is when I get the alert.

The nullity analyzer cannot see where fields are written, and does not produce a warning. Analyses of local variables are easier.

Upvotes: 1

Related Questions