0

DBAdapter.java

 public void onCreate(SQLiteDatabase db) { db.execSQL(" CREATE TABLE " + DATABASE_TABLE + " (" + KEY_FIRSTNAME + " TEXT NOT NULL, " + KEY_MIDDLENAME + " TEXT NOT NULL, " + KEY_LASTNAME + " TEXT NOT NULL, " + KEY_USERNAME + " TEXT NOT NULL, " + KEY_MAIL_ID + " TEXT NOT NULL, " + KEY_PASSWORD + " TEXT NOT NULL, " + KEY_CONFIRM + " TEXT NOT NULL, " + KEY_DATE_OF_BIRTH + " INTEGER NOT NULL " + ");"); } public String getSinlgeEntry(String userName) { Cursor cursor = mDB.query(DATABASE_TABLE,null,KEY_USERNAME+" =?",new String[]{userName},null,null,null); if (cursor.getCount() < 1) // UserName Not Exist return "NOT EXIST"; cursor.moveToFirst(); String password = cursor.getString(cursor .getColumnIndex("KEY_PASSWORD")); return password; } 

MainActivity.java

 String stored_password = dbadapter.getSinlgeEntry(username); Log.i("MainActivity", "The stored password is" + stored_password); if(password.equals(stored_password)) { Toast.makeText(MainActivity.this,"It is logging in",Toast.LENGTH_SHORT).show(); Intent intent = new Intent(MainActivity.this,Profile_view.class); startActivity(intent); } else { Toast.makeText(MainActivity.this,"Either username or password in invalid",Toast.LENGTH_LONG).show(); } 

Logcat:

12-06 16:05:47.682: E/AndroidRuntime(584): FATAL EXCEPTION: main 12-06 16:05:47.682: E/AndroidRuntime(584): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 12-06 16:05:47.682: E/AndroidRuntime(584): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:419) 12-06 16:05:47.682: E/AndroidRuntime(584): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:117) 12-06 16:05:47.682: E/AndroidRuntime(584): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:31) 12-06 16:05:47.682: E/AndroidRuntime(584): at com.example.signup.DBAdapter.getSinlgeEntry(DBAdapter.java:177) 12-06 16:05:47.682: E/AndroidRuntime(584): at com.example.signup.MainActivity$1.onClick(MainActivity.java:46) 12-06 16:05:47.682: E/AndroidRuntime(584): at android.view.View.performClick(View.java:3100) 12-06 16:05:47.682: E/AndroidRuntime(584): at android.view.View$PerformClick.run(View.java:11644) 12-06 16:05:47.682: E/AndroidRuntime(584): at android.os.Handler.handleCallback(Handler.java:587) 12-06 16:05:47.682: E/AndroidRuntime(584): at android.os.Handler.dispatchMessage(Handler.java:92) 12-06 16:05:47.682: E/AndroidRuntime(584): at android.os.Looper.loop(Looper.java:126) 12-06 16:05:47.682: E/AndroidRuntime(584): at android.app.ActivityThread.main(ActivityThread.java:3997) 12-06 16:05:47.682: E/AndroidRuntime(584): at java.lang.reflect.Method.invokeNative(Native Method) 12-06 16:05:47.682: E/AndroidRuntime(584): at java.lang.reflect.Method.invoke(Method.java:491) 12-06 16:05:47.682: E/AndroidRuntime(584): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 12-06 16:05:47.682: E/AndroidRuntime(584): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 12-06 16:05:47.682: E/AndroidRuntime(584): at dalvik.system.NativeStart.main(Native Method) 

I have 3 complete entries(firstname,middlename,lastname..) in my table. And all the username already exists in the database but it says that CursorIndexOutOfBoundException.
I have also moved the cursor to first position.

2
  • @laalto,Sorry to tell ya this but i am completely new to coding and stuffs so could you be a lil more specific as to where i should add SELECT * . Thanks anyways. Commented Dec 6, 2013 at 11:15
  • I will post a image version of my database. How do i do it ? Commented Dec 6, 2013 at 11:18

1 Answer 1

4

KEY_PASSWORD is your constant that defines the key column name, not the column name itself.

Replace

String password = cursor.getString(cursor .getColumnIndex("KEY_PASSWORD")); 

with

String password = cursor.getString(cursor .getColumnIndex(KEY_PASSWORD)); 
Sign up to request clarification or add additional context in comments.

13 Comments

I have edited it as you said. "KEY_PASSWORD" is replaced with KEY_PASSWORD. But i am still getting the same error and when i try to print the storedpassword,it returns as NOTEXIST. How is that possible ? I have entries in the table. Thanks for taking the time and looking at the code. Much appreciated.
Error is gone in the logcat, thanks to you. But stored_password still returns NOT EXISTS !! Its really weird.
Fyi, I've deleted my answer since having null as the projection/result columns paremeter in the mDB.query call doesn't appear to be the issue...
Thanks NigelK. Appreciate you taking the effort.
Thanks Szymon. Will do that. Cheers.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.