Error in database-Collection of common programming errors
Please help find the problem in my code. I’m trying to check if the user entered a valid email address and if its already existing. I already trap if the user entered invalid email address through some formatting, I already checked if the email is existing and my problem is whenever the user entered new and valid email address, my application crashed. Here’s my code:
else if (!Email.equals(""))
{
if(Email.equals(storedEmail)) //Check if the email address already exist
{
Toast.makeText(getApplicationContext(), "Email address already exist.",Toast.LENGTH_LONG).show();
}
else if (!matcherObj.matches()) //it will check if the user enter valid email
{
Toast.makeText(getApplicationContext(), "Invalid email address.",Toast.LENGTH_LONG).show();
txtEmail.setText("");
}
else // if the email address is not existing and is valid it will save to the database
{
db.Register(newPatientInfo(Fname,Mname,Lname,Suffix,Birthday,Homeno,MobileNo,Email,Brgy,Province,CityMun,Zip,CFname,CLname,DClinic,DClinicAdd));
Toast.makeText(getApplicationContext(),"Saved!", Toast.LENGTH_LONG).show();
Clear();
}
}
here is my code in my databasehandler
public String Patient_Emailexist(String p_email)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(TABLE_PATIENT, null, PATIENT_EMAIL + "=?", new String[]{String.valueOf(p_email)},null, null, null);
if (c == null)
{
c.moveToFirst();
}
c.moveToFirst();
String patient_email = c.getString(c.getColumnIndex(PATIENT_EMAIL));
return patient_email;
}
My application crash whenever I enter a valid and new email address that is supposed to be save in the database. Thanks in advance!
errors found
05-17 10:27:17.410: E/AndroidRuntime(678): FATAL EXCEPTION: main 05-17 10:27:17.410: E/AndroidRuntime(678): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 05-17 10:27:17.410: E/AndroidRuntime(678): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418) 05-17 10:27:17.410: E/AndroidRuntime(678): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 05-17 10:27:17.410: E/AndroidRuntime(678): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 05-17 10:27:17.410: E/AndroidRuntime(678): at com.example.palsproject.DatabaseHandler.Patient_Emailexist(DatabaseHandler.java:353) 05-17 10:27:17.410: E/AndroidRuntime(678): at com.example.palsproject.PatientRegistration$1.onClick(PatientRegistration.java:95) 05-17 10:27:17.410: E/AndroidRuntime(678): at android.view.View.performClick(View.java:4084) 05-17 10:27:17.410: E/AndroidRuntime(678): at android.view.View$PerformClick.run(View.java:16966) 05-17 10:27:17.410: E/AndroidRuntime(678): at android.os.Handler.handleCallback(Handler.java:615) 05-17 10:27:17.410: E/AndroidRuntime(678): at android.os.Handler.dispatchMessage(Handler.java:92) 05-17 10:27:17.410: E/AndroidRuntime(678): at android.os.Looper.loop(Looper.java:137) 05-17 10:27:17.410: E/AndroidRuntime(678): at android.app.ActivityThread.main(ActivityThread.java:4745) 05-17 10:27:17.410: E/AndroidRuntime(678): at java.lang.reflect.Method.invokeNative(Native Method) 05-17 10:27:17.410: E/AndroidRuntime(678): at java.lang.reflect.Method.invoke(Method.java:511) 05-17 10:27:17.410: E/AndroidRuntime(678): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 05-17 10:27:17.410: E/AndroidRuntime(678): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 05-17 10:27:17.410: E/AndroidRuntime(678): at dalvik.system.NativeStart.main(Native Method)