Home > Mobile >  int android.database.Cursor.getColumnIndex(java.lang.String)' on a null object reference
int android.database.Cursor.getColumnIndex(java.lang.String)' on a null object reference

Time:12-03

public class MainActivity extends AppCompatActivity {

    TextView dataTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dataTextView = findViewById(R.id.dataTextView);

        FloatingActionButton floatingActionButton = findViewById(R.id.floatingActionButton);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, add_member.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        displayData();
    }

    private void displayData () {
        String [] projection = {
                MemberEntry._ID,
                MemberEntry.COLUMN_FIRST_NAME,
                MemberEntry.COLUMN_LAST_NAME,
                MemberEntry.COLUMN_GENDER,
                MemberEntry.COLUMN_SPORT
        };
        Cursor cursor = getContentResolver().query(
                MemberEntry.CONTENT_URI,
                projection,
                null,
                null,
                null
        );

        dataTextView.setText("All members\n\n");
        dataTextView.append(MemberEntry._ID   " "  
                MemberEntry.COLUMN_FIRST_NAME   " "  
                MemberEntry.COLUMN_LAST_NAME   " "  
                MemberEntry.COLUMN_GENDER   " "  
                MemberEntry.COLUMN_SPORT);

        int idIndex = cursor.getColumnIndex(MemberEntry._ID);
        int idFirstName = cursor.getColumnIndex(MemberEntry.COLUMN_LAST_NAME);
        int idLastName = cursor.getColumnIndex(MemberEntry.COLUMN_LAST_NAME);
        int idGender = cursor.getColumnIndex(MemberEntry.COLUMN_GENDER);
        int idSport = cursor.getColumnIndex(MemberEntry.COLUMN_SPORT);

        while (cursor.moveToNext()) {
            int currentID = cursor.getInt(idIndex);
            String currentFirstName = cursor.getString(idFirstName);
            String currentLastName = cursor.getString(idLastName);
            int currentGender = cursor.getInt(idGender);
            String currentSport = cursor.getString(idSport);

            dataTextView.append("\n"  
                    currentID   " "  
                    currentFirstName   " "  
                    currentLastName   " "  
                    currentGender   " "  
                    currentSport);
        }

        cursor.close();

    }
}

This throws an exception:

java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getColumnIndex(java.lang.String)' on a null object reference
    at com.example.sportclub.MainActivity.displayData(MainActivity.java:66)
    at com.example.sportclub.MainActivity.onStart(MainActivity.java:40)

can you help me ?)

CodePudding user response:

It looks like the line cursor.getColumnIndex(MemberEntry._ID); is the problem. In all likelihood given the error message cursor is null and that your call to getContentResolver().query failed.

CodePudding user response:

private void insertMember () {
        String firstName = firstNameEditText.getText().toString().trim();
        String lastName = lastNameEditText.getText().toString().trim();
        String sport = sportEditText.getText().toString().trim();

        ContentValues contentValues = new ContentValues();
        contentValues.put(MemberEntry.COLUMN_FIRST_NAME, firstName);
        contentValues.put(MemberEntry.COLUMN_LAST_NAME, lastName);
        contentValues.put(MemberEntry.COLUMN_SPORT, sport);
        contentValues.put(MemberEntry.COLUMN_GENDER, gender);

        ContentResolver contentResolver = getContentResolver();
        Uri uri = contentResolver.insert(MemberEntry.CONTENT_URI, contentValues);

        if (uri == null ) {
            Toast.makeText(this, "Insertion of data in the table failed for ", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "Data save ", Toast.LENGTH_LONG).show();
        }
    }
}

this is correct code ? or not ? 
  • Related