3

I have called a function returnCurrentNameById() by passing the id of the particular row. But it gives an error.

String[] s1;

public String[] returnCurrentNameById(int k) throws SQLException{  

    String[] columns = new String[]{ KEY_ID1, KEY_NAME, Key_DOB, KEY_AGE, KEY_PHONE_NO, Key_EXPERIENCE, KEY_EMAIL, KEY_STATUS, KEY_STATUS};
    SQLiteDatabase db3 = this.getReadableDatabase();
    Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + "=" + k, null, null, null, null);

    if (c != null){
        c.moveToFirst();
        for(int i = 0; i<=8; i++){
            s1[i] = c.getString(i);
        }
        return s1;
    }
    return null;
}

The Log cat error report is:

02-15 16:38:13.424    1545-1545/world.com.my`enter code here`progect09 D/AndroidRuntime﹕ Shutting down VM
02-15 16:38:13.464    1545-1545/world.com.myprogect09 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x2bc9a300)
02-15 16:38:13.533    1545-1545/world.com.myprogect09 E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3591)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3586)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at world.com.myprogect09.DataBaseSQL.returnCurrentNameById1(DataBaseSQL.java:209)
            at world.com.myprogect09.ThirdActivity.isClicked(ThirdActivity.java:74)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3586)
            at android.view.View.performClick(View.java:4084)
            at android.view.View$PerformClick.run(View.java:16966)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
02-15 16:38:34.491    1545-1545/world.com.myprogect09 I/Process﹕ Sending signal. PID: 1545 SIG: 9
2
  • What's the error it throws? Show your logcat Commented Feb 15, 2015 at 10:26
  • In the headline you state that a string shall be stored, but it seems that the code does only read the database. Could you please fix it? Commented Feb 15, 2015 at 18:43

2 Answers 2

2

You have not initialized your s1 array.

Use

s1 = new String[9];

to allocate an array of 9 elements.

Also check the return value of moveToFirst() so you don't get an exception in case the query returns no rows.

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

1 Comment

Thanks sir, the problem was the string s1[] which I was not initialized with new operator. Therefor, NullPointerException occurs
0

Replace

 Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + "=" + k, null, null, null, null);

with

 Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + " = ?", new String[]{String.valueOf(k)}, null, null, null);

3 Comments

Sorry the problem still there, it gives AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity
"IllegalStateException: Could not execute method of the activity" can caused due to lot of reasons. please post your log cat
Something in line 209 on DataBaseSQL.class is null. check what it is

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.