title: Couldn't read row from CursorWindow
body: <p>I'm doing an Android application in which I want to implement a registration feature where a new user is added.</p>
  
  <p>At first I have created an activity called Registration, and when the button is pressed it calls a method that add a user:</p>
  
  <pre><code>public User createUser(String username, String password, String email) {
              ContentValues values = new ContentValues();
              values.put(MySQLiteHelperUsers.COLUMN_USERNAME, username);
              values.put(MySQLiteHelperUsers.COLUMN_PASSWORD, password);
              values.put(MySQLiteHelperUsers.COLUMN_EMAIL, email);
              long insertId = database.insert(MySQLiteHelperUsers.TABLE_USERS, null,
                  values);
              Cursor cursor = database.query(MySQLiteHelperUsers.TABLE_USERS,
                  allColumns, MySQLiteHelperUsers.COLUMN_ID + " = " + insertId, null,
                  null, null, null);
              cursor.moveToFirst();
              User newUser = cursorToUser(cursor);
              cursor.close();
              return newUser;
  }
  </code></pre>
  
  <p>And here is cursorToUser method:</p>
  
  <pre><code>private User cursorToUser(Cursor cursor) {
          User user = new User();
          user.setId(cursor.getLong(cursor.getColumnIndex("_id")));
          user.setUsername(cursor.getString(cursor.getColumnIndex("_username")));
          user.setPassword(cursor.getString(cursor.getColumnIndex("_password")));
          user.setEmail(cursor.getString(cursor.getColumnIndex("_email")));
          return user;
  }
  </code></pre>
  
  <p>But it gives me this errors:</p>
  
  <pre><code>    05-10 13:09:35.081: E/AndroidRuntime(16380): FATAL EXCEPTION: main
  05-10 13:09:35.081: E/AndroidRuntime(16380): java.lang.IllegalStateException: Could not execute method of the activity
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.view.View$1.onClick(View.java:3704)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.view.View.performClick(View.java:4232)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.view.View$PerformClick.run(View.java:17298)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.os.Handler.handleCallback(Handler.java:615)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.os.Handler.dispatchMessage(Handler.java:92)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.os.Looper.loop(Looper.java:137)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.app.ActivityThread.main(ActivityThread.java:4921)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at java.lang.reflect.Method.invokeNative(Native Method)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at java.lang.reflect.Method.invoke(Method.java:511)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at dalvik.system.NativeStart.main(Native Method)
  05-10 13:09:35.081: E/AndroidRuntime(16380): Caused by: java.lang.reflect.InvocationTargetException
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at java.lang.reflect.Method.invokeNative(Native Method)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at java.lang.reflect.Method.invoke(Method.java:511)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.view.View$1.onClick(View.java:3699)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    ... 11 more
  05-10 13:09:35.081: E/AndroidRuntime(16380): 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.
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.database.CursorWindow.nativeGetLong(Native Method)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.database.CursorWindow.getLong(CursorWindow.java:511)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at dao.UsersDataSource.cursorToUser(UsersDataSource.java:75)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at dao.UsersDataSource.createUser(UsersDataSource.java:44)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    at com.example.pocket_city.Registration.registrationClick(Registration.java:47)
  05-10 13:09:35.081: E/AndroidRuntime(16380):    ... 14 more
  05-10 13:09:35.141: D/dalvikvm(16380): GC_CONCURRENT freed 177K, 14% free 9641K/11143K, paused 15ms+4ms, total 79ms
  </code></pre>
  
  <p>Here is the MySQLiteHelperUsers class:</p>
  
  <pre><code>public class MySQLiteHelperUsers extends SQLiteOpenHelper{
      public static final String TABLE_USERS = "users";
      public static final String COLUMN_ID = "_id";
      public static final String COLUMN_USERNAME = "_username";
      public static final String COLUMN_PASSWORD = "_password";
      public static final String COLUMN_EMAIL = "_email";
  
      private static final String DATABASE_NAME = "users.db";
      private static final int DATABASE_VERSION = 1;
  
      // Database creation sql statement
      private static final String DATABASE_CREATE = "CREATE TABLE "
              + TABLE_USERS.trim() + "(" 
              + COLUMN_ID.trim() + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
              + COLUMN_USERNAME.trim() + " TEXT NOT NULL, "
              + COLUMN_PASSWORD.trim() + " TEXT NOT NULL, "
              + COLUMN_EMAIL.trim()  
              + " TEXT NOT NULL)";
  
      public MySQLiteHelperUsers(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase database) {
          database.execSQL(DATABASE_CREATE);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
          onCreate(db);
      }
  </code></pre>
  
  <p>Registration activity: </p>
  
  <pre><code>    public class Registration extends Activity {
          UsersDataSource usersDataSource = null;
          User user = null;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.registration);
  
  
              usersDataSource = new UsersDataSource(getApplicationContext());
              usersDataSource.open();
          }
  
  
          public void registrationClick(View view){
              EditText editUsername = (EditText) findViewById(R.id.editUsername);
                  String username = editUsername.getText().toString();
              EditText editPassw = (EditText) findViewById(R.id.editPassw);
                  String passw = editPassw.getText().toString();
              EditText editPassw2 = (EditText) findViewById(R.id.editPassw2);
                  String passw2 = editPassw2.getText().toString();
              EditText editEmail = (EditText) findViewById(R.id.editEmail);
                  String email = editEmail.getText().toString();
              EditText editEmail2 = (EditText) findViewById(R.id.editEmail2);
                  String email2 = editEmail2.getText().toString();
              System.out.println("PASSW= "+passw+" "+passw2);
              System.out.println("EMAIL= "+email+" "+email2);
  
              /*if(!passw.equals(passw2) &amp;&amp; !email.equals(email2)){
                  Toast.makeText(getApplicationContext(), "Le password/email non sono uguali!", Toast.LENGTH_LONG).show();
              }*/
  
              usersDataSource.createUser(username, passw, email);
              /*Toast.makeText(getApplicationContext(), "Registrazione riuscita!", Toast.LENGTH_LONG).show();*/       
  
  
          }    
  }
  </code></pre>