Skip to content

Instantly share code, notes, and snippets.

@Josephaguele
Created June 1, 2017 04:51
Show Gist options
  • Save Josephaguele/27e776bbb504d6ee9d03bc54ce0d22e4 to your computer and use it in GitHub Desktop.
Save Josephaguele/27e776bbb504d6ee9d03bc54ce0d22e4 to your computer and use it in GitHub Desktop.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.pets/com.example.android.pets.CatalogActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int android.database.Cursor.getCount()' on a null object reference
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.pets;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import static com.example.android.pets.data.PetContract.PetEntry.COLUMN_PET_BREED;
import static com.example.android.pets.data.PetContract.PetEntry.COLUMN_PET_GENDER;
import static com.example.android.pets.data.PetContract.PetEntry.COLUMN_PET_NAME;
import static com.example.android.pets.data.PetContract.PetEntry.COLUMN_PET_WEIGHT;
import static com.example.android.pets.data.PetContract.PetEntry.CONTENT_URI;
import static com.example.android.pets.data.PetContract.PetEntry.GENDER_MALE;
import static com.example.android.pets.data.PetContract.PetEntry._ID;
/**
* Displays list of pets that were entered and stored in the app.
*/
public class CatalogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
// Setup FAB to open EditorActivity
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
displayDatabaseInfo();
}
/**
* Temporary helper method to display information in the onscreen TextView about the state of
* the pets database.
*/
private void displayDatabaseInfo() {
// Define a projection that specifies which columns from the database
// you will actually use after this query.
String[] projection = {_ID,
COLUMN_PET_NAME,
COLUMN_PET_BREED,
COLUMN_PET_GENDER,
COLUMN_PET_WEIGHT};
// Perform a query on the provider using the ContentResolver.
// Use the {"link PetEntry#CONTENT_URI} to access the pet data.
Cursor cursor = getContentResolver().query(CONTENT_URI, // The content URI of the words table
projection, // The content URI with the words fill
null, // Selection criteria
null, // Selection criteria
null); // The sort order for the returned rows.
TextView displayView = (TextView) findViewById(R.id.text_view_pet);
try {
// Create a header in the Text View that looks like this:
//
// The pets table contains <number of rows in Cursor> pets
// _id - name - breed - gender - weight
//
// IN the while loop below, iterate through the rows of the cursor and display the information
// from each column in this order
displayView.setText("The pets table contains " + cursor.getCount() + " pets.\n\n");
displayView.append(_ID + " - " +
COLUMN_PET_NAME + " - " +
COLUMN_PET_BREED + " - " +
COLUMN_PET_GENDER + " - " +
COLUMN_PET_WEIGHT + "\n");
// Figure out the index of each column
int idColumnIndex = cursor.getColumnIndex(_ID);
int nameColumnIndex = cursor.getColumnIndex(COLUMN_PET_NAME);
int breedColumnIndex = cursor.getColumnIndex(COLUMN_PET_BREED);
int genderColumnIndex = cursor.getColumnIndex(COLUMN_PET_GENDER);
int weightColumnIndex = cursor.getColumnIndex(COLUMN_PET_WEIGHT);
// Iterate through all the returned rows in the cursor.
while (cursor.moveToNext()) {
//Use that index to extract the String or int value of the word
// at the current row the cursor is on.
int currentID = cursor.getInt(idColumnIndex);
String currentName = cursor.getString(nameColumnIndex);
String currentBreed = cursor.getString(breedColumnIndex);
int currentGender = cursor.getInt(genderColumnIndex);
int currentWeight = cursor.getInt(weightColumnIndex);
//Display the values from each column of the current row in the cursor in the TextView
displayView.append(("\n" + currentID + " - " +
currentName + " - " +
currentBreed + " - " +
currentGender + " - " +
currentWeight));
}
} finally {
// Always close the cursor when you're done reading from it. This releases all its
// resources and makes it invalid.
cursor.close();
}
}
private void insertPet() {
ContentValues values = new ContentValues();
values.put(COLUMN_PET_NAME, "Toto");
values.put(COLUMN_PET_BREED, "Terrier");
values.put(COLUMN_PET_GENDER, GENDER_MALE);
values.put(COLUMN_PET_WEIGHT, 7);
Uri newUri = getContentResolver().insert(CONTENT_URI, values);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu options from the res/menu/menu_catalog.xml file.
// This adds menu items to the app bar.
getMenuInflater().inflate(R.menu.menu_catalog, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
// Respond to a click on the "Insert dummy data" menu option
case R.id.action_insert_dummy_data:
insertPet();
displayDatabaseInfo();
return true;
// Respond to a click on the "Delete all entries" menu option
case R.id.action_delete_all_entries:
// Do nothing for now
return true;
}
return super.onOptionsItemSelected(item);
}
}
@Josephaguele
Copy link
Author

In my PetsApp, the android Run monitor keeps showing me this error.
error

@Josephaguele
Copy link
Author

@dagger
@Alex_234
This is the CatalogActivity for the pets app.
Please this code is giving me an issue as shown in the Android monitor. Please could you help out with it.

@naveenkumargupta1997
Copy link

How can u use content provider without contract class . I think you need to define it .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment