Force close in reading sqlite database
<p>I am trying to read the data from my sqlite database but getting force close while fetching data from table. Can anyone help. Thanks..</p>

<p>Here is my code</p>

<pre><code> public Key getKey(String phone_no) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_KEY, new String[] { 
            KEY_VALUE, KEY_PH_NO }, KEY_PH_NO + "=?",
            new String[] { phone_no }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Key contact = new Key(cursor.getString(0),
            cursor.getString(1));
    // return contact
    return contact;
}
</code></pre>

<p>Application is getting force closed in line</p>

<pre><code>Key contact = new Key(cursor.getString(0),
                cursor.getString(1));
</code></pre>

<p>Here is my log cat</p>

<pre><code>06-01 11:50:33.308: D/AndroidRuntime(307): Shutting down VM
06-01 11:50:33.308: W/dalvikvm(307): threadid=1: thread exiting with uncaught exception     (group=0x4001d800)
06-01 11:50:33.327: E/AndroidRuntime(307): FATAL EXCEPTION: main
06-01 11:50:33.327: E/AndroidRuntime(307): java.lang.RuntimeException: Unable to start     activity             ComponentInfo{net.learn2develop.SMSMessaging/net.learn2develop.SMSMessaging.SqlQuery}:     android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
06-01 11:50:33.327: E/AndroidRuntime(307):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-01 11:50:33.327: E/AndroidRuntime(307):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-01 11:50:33.327: E/AndroidRuntime(307):  at     android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.os.Looper.loop(Looper.java:123)
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-01 11:50:33.327: E/AndroidRuntime(307):  at     java.lang.reflect.Method.invokeNative(Native Method)
06-01 11:50:33.327: E/AndroidRuntime(307):  at java.lang.reflect.Method.invoke(Method.java:521)
06-01 11:50:33.327: E/AndroidRuntime(307):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-01 11:50:33.327: E/AndroidRuntime(307):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-01 11:50:33.327: E/AndroidRuntime(307):  at dalvik.system.NativeStart.main(Native Method)
06-01 11:50:33.327: E/AndroidRuntime(307): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
06-01 11:50:33.327: E/AndroidRuntime(307):  at net.learn2develop.SMSMessaging.DatabaseHandler.getKey(DatabaseHandler.java:72)
06-01 11:50:33.327: E/AndroidRuntime(307):  at net.learn2develop.SMSMessaging.SqlQuery.onCreate(SqlQuery.java:19)
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-01 11:50:33.327: E/AndroidRuntime(307):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-01 11:50:33.327: E/AndroidRuntime(307):  ... 11 more
06-01 11:50:37.058: I/Process(307): Sending signal. PID: 307 SIG: 9
</code></pre>
