Reputation: 69
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)
Upvotes: 0
Views: 84
Reputation: 916
what you are doing is you are checking whether cursor is null even it is null also the statement after if will be ececuted so there it will be null therefor outofbound for getstring. i have modified your code
public String Patient_Emailexist(String p_email)
{
String patient_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)
{
return patient_email;
}
else
{ c.moveToFirst();
String patient_email = c.getString(c.getColumnIndex(PATIENT_EMAIL));
}
return patient_email;
}
i have initialized patient_email to "" in the beginning so is it is null "" will be returned else the string
Upvotes: 0
Reputation: 15009
The problems are here:
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));
First, if c equals null, c.moveToFirst()
is going to crash, but that's a separate issue.
Next, the online help for getString()
says:
The result and whether this method throws an exception when the column value is null or the column type is not a string type is implementation-defined.
As you say, the record is not in the database, so the column value will be null
. You perhaps want:
String patient_email = null;
if (!c.isNull(c.getColumnIndex(PATIENT_EMAIL)) && c.getType (c.getColumnIndex(PATIENT_EMAIL)) == FIELD_TYPE_STRING)
patient_email = c.getString(c.getColumnIndex(PATIENT_EMAIL));
Upvotes: 1