mardi 29 septembre 2015

How get row counts of a table sqlite in android

I tried this code but it will always go error : java.lang.NullPointerException

I dont know why. I have records in my table Goals.

This is my code.

public long getGoalsCount() {
        SQLiteDatabase db=dbhandler.getWritableDatabase();
        long numRows = DatabaseUtils.queryNumEntries(db, MyDBHandler.TABLE_GOALS);
        return numRows;
    }

When i called out the function

public void displayAllGoals(){
        long goal_counts = dbhandler.getGoalsCount(); // The error points her that it means it doesnt return any value. And that it's null
        MessageTo.message(ViewGoalsActivity.this,Long.toString(goal_counts)); //To display the value

    }

OnCreate Function

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_goals);

        displayAllGoals();

    }

I can pinpoint what's wrong in my code. I tried other sources but its still the same error. I'm a newbie in Android.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.citu.organizer/com.citu.organizer.ViewGoalsActivity}: java.lang.NullPointerException

at com.citu.organizer.ViewGoalsActivity.displayAllGoals(ViewGoalsActivity.java:57)
            at com.citu.organizer.ViewGoalsActivity.onCreate(ViewGoalsActivity.java:38)

Aucun commentaire:

Enregistrer un commentaire