title: getting column contents using cursor
body: <p>I have a Cursor returned From a SQLiteDataBase object , I am using cursor getXXX()method to get the content of columns from the cursor but I am getting error.
  My code is where I fetch the cursor from database and get the object...</p>
  
  <pre><code>public List&lt;Message&gt; getAllMessages(char ch) {
        Cursor cursor = null;
      List&lt;Message&gt; message = new ArrayList&lt;Message&gt;();
  if(ch=='r')
      {
      String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE " +MySQLiteHelper.COLUMN_STATUS + "= 'r'";
  cursor = database.rawQuery(queryz, null);
  }
  if(ch=='s')
  {
  String queryz = "SELECT " + MySQLiteHelper.COLUMN_ID + "," + MySQLiteHelper.COLUMN_MESSAGE + " FROM " + MySQLiteHelper.TABLE_NAME + " WHERE " +MySQLiteHelper.COLUMN_STATUS + "= 's'";
  cursor = database.rawQuery(queryz, null);
  }
  
  
     cursor.moveToFirst();
      while (!cursor.isAfterLast()) {
        Message message1 = cursorToMessage(cursor);
        message.add(message1);
        cursor.moveToNext();
      }
      // Make sure to close the cursor
      cursor.close();
      return message;
    }
  
      public Message createMessage(String message,char ch,int random) {
          ContentValues values = new ContentValues();
  
          if(ch=='s')
          values.put(MySQLiteHelper.COLUMN_STATUS, "s");
      if(ch=='r')
          values.put(MySQLiteHelper.COLUMN_STATUS, "r");
  
          values.put(MySQLiteHelper.COLUMN_PARENTID, 0);
  
          values.put(MySQLiteHelper.COLUMN_THREADID, random);
  
          values.put(MySQLiteHelper.COLUMN_MESSAGE, message);
          long insertId = database.insert(MySQLiteHelper.TABLE_NAME, null,
              values);
  
  
         Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
              allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
              null, null, null);
         /* Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
                  allColumns, null, null,
                  null, null, null);*/
         cursor.moveToFirst();
          Message newMessage = cursorToMessage(cursor);
         cursor.close();
          return newMessage;
        }
           private Message cursorToMessage(Cursor cursor) {
              Message message = new Message();
              message.setId(cursor.getLong(cursor.getColumnIndex("id")));
  
              message.setmessage(cursor.getString(cursor.getColumnIndex("message")));
              message.setthreadid(cursor.getLong(cursor.getColumnIndex("threadid")));
  
              return message;
            }
  
      My log-cat is:
  
          09-11 17:15:01.227: E/AndroidRuntime(4707): FATAL EXCEPTION: main
          09-11 17:15:01.227: E/AndroidRuntime(4707): java.lang.RuntimeException: Unable to start activity ComponentInfo{soft.b.peopleassist/soft.b.peopleassist.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.os.Handler.dispatchMessage(Handler.java:99)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.os.Looper.loop(Looper.java:137)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.main(ActivityThread.java:4340)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at java.lang.reflect.Method.invokeNative(Native Method)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at java.lang.reflect.Method.invoke(Method.java:511)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at dalvik.system.NativeStart.main(Native Method)
          09-11 17:15:01.227: E/AndroidRuntime(4707): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.database.CursorWindow.nativeGetLong(Native Method)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.database.CursorWindow.getLong(CursorWindow.java:526)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at soft.b.peopleassist.DataSource.cursorToMessage(DataSource.java:104)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at soft.b.peopleassist.DataSource.getAllMessages(DataSource.java:89)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at soft.b.peopleassist.Send.onCreateView(Send.java:58)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:795)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:998)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.BackStackRecord.run(BackStackRecord.java:622)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1330)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.Activity.performStart(Activity.java:4474)
          09-11 17:15:01.227: E/AndroidRuntime(4707):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1928)
  </code></pre>