1

I am getting null pointer exception while fetching the data from the SQLite table. I am getting the null pointer exception when fetching the data using cursor like below Cursor c= db.fetchAllData(); Please have a look at the below precise code, and suggest me some ideas...

display_transaction.class

public class display_transaction extends Activity {

    DisplayTestDBHelper db;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_listview);

        displayListView();
    }

    public void displayListView() {
        Cursor c= db.fetchAllData();
        String[] columns={db.KEY_ID,db.KEY_DESCRIPTION,db.KEY_AMOUNT};
        int[] id=new int[]{R.id.textView1,R.id.textView2};
        SimpleCursorAdapter s=new SimpleCursorAdapter(this,R.layout.listviewlayout,c,columns,id,0);          

        ListView l=(ListView) findViewById(R.id.listview1);
        l.setAdapter(s);
    }
}

DisplayTestDBHelper.class

public Cursor fetchAllData() {
    String TABLE_NAME="transaction_table";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor mCursor = db.query(TABLE_NAME, new String[] {KEY_ID, KEY_DESCRIPTION, KEY_AMOUNT}, null, null, null, null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;


}

Please help me to resolve the above issue... Thanks in advance!!!!

1
  • from now on, if you get NullPointerException, first go and search if you have initialized an object or not. That will save your alot of time. Commented Aug 10, 2014 at 10:41

2 Answers 2

5

You didn't initialize db.

Depending on how you've implemented the helper's constructor, use something like

db = new DisplayTestDBHelper(this);

to init it, passing the activity this for a Context.

Sign up to request clarification or add additional context in comments.

Comments

2

You haven't initialized db.

You need to do something like: db = new DisplayTestDBHelper(this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.