1

Searched a whole bunch of the same question and i couldn't get an idea why i get NPE. Pls help.

My code to read data from a table is,

private String []cols = {"name","id"};

public void seeRecord(String a, int b){



    Cursor c = db.rawQuery("select * from vijay", null);
    c.moveToFirst();
    while(c.moveToNext()){
    String nameintable = c.getString(c.getColumnIndex(cols[0]));

    int idintable = c.getInt(c.getColumnIndex(cols[1]));

    a=nameintable;
    b=idintable;

}
    c.close();
}

To insert data to the table is:

public void addRecord(String name, int id){
    db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("name",name);
    values.put("id", id);

    db.insert("vijay", null, values);
    db.close();
    }

logcat errors:

07-30 12:11:34.755: E/AndroidRuntime(15894): FATAL EXCEPTION: main
07-30 12:11:34.755: E/AndroidRuntime(15894): java.lang.IllegalStateException: Could not execute method of the activity
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.view.View$1.onClick(View.java:2072)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.view.View.performClick(View.java:2408)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.view.View$PerformClick.run(View.java:8816)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.os.Handler.handleCallback(Handler.java:587)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.os.Looper.loop(Looper.java:123)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at dalvik.system.NativeStart.main(Native Method)
07-30 12:11:34.755: E/AndroidRuntime(15894): Caused by: java.lang.reflect.InvocationTargetException
07-30 12:11:34.755: E/AndroidRuntime(15894):    at com.example.databaseaccess.MainActivity.see(MainActivity.java:42)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.view.View$1.onClick(View.java:2067)
07-30 12:11:34.755: E/AndroidRuntime(15894):    ... 11 more
07-30 12:11:34.755: E/AndroidRuntime(15894): Caused by: java.lang.NullPointerException
07-30 12:11:34.755: E/AndroidRuntime(15894):    at com.example.databaseaccess.DbHelper.seeRecord(DbHelper.java:61)
07-30 12:11:34.755: E/AndroidRuntime(15894):    ... 15 more
07-30 12:23:40.606: E/AndroidRuntime(18817): FATAL EXCEPTION: main
07-30 12:23:40.606: E/AndroidRuntime(18817): java.lang.IllegalStateException: Could not execute method of the activity
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.view.View$1.onClick(View.java:2072)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.view.View.performClick(View.java:2408)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.view.View$PerformClick.run(View.java:8816)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.os.Handler.handleCallback(Handler.java:587)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.os.Looper.loop(Looper.java:123)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at dalvik.system.NativeStart.main(Native Method)
07-30 12:23:40.606: E/AndroidRuntime(18817): Caused by: java.lang.reflect.InvocationTargetException
07-30 12:23:40.606: E/AndroidRuntime(18817):    at com.example.databaseaccess.MainActivity.see(MainActivity.java:42)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.view.View$1.onClick(View.java:2067)
07-30 12:23:40.606: E/AndroidRuntime(18817):    ... 11 more
07-30 12:23:40.606: E/AndroidRuntime(18817): Caused by: java.lang.NullPointerException
07-30 12:23:40.606: E/AndroidRuntime(18817):    at com.example.databaseaccess.DbHelper.seeRecord(DbHelper.java:61)
07-30 12:23:40.606: E/AndroidRuntime(18817):    ... 15 more
07-30 12:23:49.245: E/AndroidRuntime(18861): FATAL EXCEPTION: main
07-30 12:23:49.245: E/AndroidRuntime(18861): java.lang.IllegalStateException: Could not execute method of the activity
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.view.View$1.onClick(View.java:2072)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.view.View.performClick(View.java:2408)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.view.View$PerformClick.run(View.java:8816)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.os.Handler.handleCallback(Handler.java:587)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.os.Looper.loop(Looper.java:123)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at dalvik.system.NativeStart.main(Native Method)
07-30 12:23:49.245: E/AndroidRuntime(18861): Caused by: java.lang.reflect.InvocationTargetException
07-30 12:23:49.245: E/AndroidRuntime(18861):    at com.example.databaseaccess.MainActivity.see(MainActivity.java:42)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.view.View$1.onClick(View.java:2067)
07-30 12:23:49.245: E/AndroidRuntime(18861):    ... 11 more
07-30 12:23:49.245: E/AndroidRuntime(18861): Caused by: java.lang.IllegalStateException: database not open
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1333)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at com.example.databaseaccess.DbHelper.seeRecord(DbHelper.java:61)
07-30 12:23:49.245: E/AndroidRuntime(18861):    ... 15 more

both are called in onclick event of a button. Inserting works properly. But when clicking read button, app crashes. Where is the mistake?

5
  • at com.example.databaseaccess.DbHelper.seeRecord(DbHelper.java:61) There is an object at line 61 which is null. Please find this line yourself and debug to see what object is null Commented Jul 30, 2013 at 7:05
  • while(c.moveToNext()) is that line pal. Does that mean there is nothing in the db stored? Commented Jul 30, 2013 at 7:08
  • No, moveToNext() returns boolean value so It will never be null. I guess that C instance is null. Please check if you opened Database successful. In the case you have no records, c.moveToNext() just returns false. Commented Jul 30, 2013 at 7:13
  • now the app don't crash. but the value returned is, '0.null'. So no records in table (or) mistake in reading? Commented Jul 30, 2013 at 7:16
  • Please try my solution at below answer. Commented Jul 30, 2013 at 7:26

5 Answers 5

3

I always use this way:

public List<Ticket> getTickets() {
        openDataBase();
        Cursor cursor = myDataBase.rawQuery("Select * from ABC" , null);
        while (cursor.moveToNext()) {
            //parse data here
        }
        cursor.close();
        closeDatabase();
        return tickets;
    }

In the case above codes don't fix your problem, please try this:

public List<Ticket> getTickets() {
    openDataBase();
    Cursor cursor = myDataBase.rawQuery("Select * from ABC" , null);
    while (!cursor.isAfterLast()) {
        cursor.moveToNext();
        //parse data here
    }
    cursor.close();
    closeDatabase();
    return tickets;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Now its working pal. Thank you so much. But it just shows the just entered value. How to retrieve those already saved?
3

Try this

public void seeRecord(String a, int b){

    db = this.getReadableDatabase();

    Cursor c = db.rawQuery("select * from vijay", null);
    c.moveToFirst();
    while(c.moveToNext()){
        String nameintable = c.getString(c.getColumnIndex(cols[0]));

        int idintable = c.getInt(c.getColumnIndex(cols[1]));

        a=nameintable;
        b=idintable;

    }
    c.close();
}

4 Comments

This is how it looks now. App doesn't crash but returns "0. null" as the value.
are you sure there is at least 1 record at vijay table?
@NguyenMinhBinh: How to check that? If the insert function works properly, there should be a lot of records in it.
No Asha. Just displaying what we just entered into db. Or displays null,0.
1

You might want to post the logcat to make this debug easier. But based off that code I'm assuming your db is null.

In your insert you call these 2 methods.

db = this.getWritableDatabase();

db.close();

I'm assumming it's crashing on this line.

Cursor c = db.rawQuery("select * from vijay", null);

Which means you will need to reopen /close the database like you did in the insert.

Comments

1

try

public void seeRecord(String a, int b){
    db = this.getWritableDatabase();
    Cursor c = db.rawQuery("select * from vijay", null);
    c.moveToFirst();
    while(c.moveToNext()){
    String nameintable = c.getString(c.getColumnIndex(cols[0]));

    int idintable = c.getInt(c.getColumnIndex(cols[1]));

    a=nameintable;
    b=idintable;
   }
    c.close();
    db.close();
}

3 Comments

didn't work yet pal. app crashes. i suspect cursor part to be the place where the mistake lies.
Tried pal. returned 0.null
@VijayaRagavan here is a=nameintable; b=idintable; something wrong ! try by testing Log.d("test",nameintable+" "+idintable);
1

Replace your code with this

public Cursor seeRecord()
{
    db.open(); 
    Cursor c = db.rawQuery("select * from vijay", null);
    db.close();

    return c;
}

write following code on your method where you want to upadte your view

db.open(); 
Cursor c = seeRecord();

 c.moveToFirst();

while(!c.isAfterLast())
{
    if(c != null)
    {
        String nameintable = c.getString(0);
        int idintable = c.getInt(1);

        Your_View.id.setText(nameintable);
        Your_View.id.setText(idintable);
    }
    c.moveToNext();
}
c.close();  
db.close();

5 Comments

Sorry David. App crashes.
No i didn't. carelesness. Now after inserting that, null value is returned. That too the initialized values of the variables where the values from the cursor is stored.
It ain't david. I initialized some two variables to be null and 0. The values got from the cursor is stored to these variables. And the textview is updated with these variable values.
But instead of the values returned from the cursor getting printed, the default assigned values are printed. that means no values returned from the cursor?
you can handle this null or 0 by if condition.

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.