I get a StringIndexOutOfBoundsException while using substring in Android Studio - android

This line of code puts my three inputs for a phone number( the inputs split he phone number into area code, first three digits and the following four digits) into a string fullPhoneNumber. I looked through a few answers before asking but I couldn't adapt them to my code.
fullPhoneNumber = areaCodeInput.getText().toString() + firstThreeDigitsInput.getText().toString() + finalFourDigitsInput.getText().toString();
Next this code enters the full string into a database.
LoginEntries newPhoneNumber = new LoginEntries("", "", "", "","", fullPhoneNumber);
This piece of code is called when i want to print the phone number in an activity. I use substring in this method the display the phone number in the format(888)-888-8888. But there is always an error. dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 3) + '-' + holder.substring(7, 4);
public String phoneNumberDatabaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE 1";
//cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex(COLUMN_PHONENUMBER)) != null){
String holder = c.getString(c.getColumnIndex(COLUMN_PHONENUMBER));
dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 3) + '-' + holder.substring(7, 4);
dbString += "\n";
}
c.moveToNext();
}
db.close();
c.close();
return dbString;
}
This is the error if I try the substring as I found it on stackoverflow.
Process: com.example.vitaliy_2.emailpassworddatabasetrial, PID: 17397
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5191)  at android.view.View$PerformClick.run(View.java:20916) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:145)  at android.app.ActivityThread.main(ActivityThread.java:5972 at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: java.lang.StringIndexOutOfBoundsException: length=12; regionStart=4; regionLength=-1
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at com.example.vitaliy_2.emailpassworddatabasetrial.EmailDBHandler.phoneNumberDatabaseToString(EmailDBHandler.java:257)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.printDatabase(SignUpMainClass.java:183)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.registerAccount(SignUpMainClass.java:153)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
at android.view.View.performClick(View.java:5191) 
at android.view.View$PerformClick.run(View.java:20916)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5972) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.Zyg`enter code here`oteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
I changed the code a bit hoping it would help but to no avail. dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 6) + '-' + holder.substring(7, 10);
public String phoneNumberDatabaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE 1";
//cursor point to a location in your results
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex(COLUMN_PHONENUMBER)) != null){
String holder = c.getString(c.getColumnIndex(COLUMN_PHONENUMBER));
dbString = '(' + holder.substring( 1, 3) + ')' + '-' + holder.substring(4, 6) + '-' + holder.substring(7, 10);
dbString += "\n";
}
c.moveToNext();
}
db.close();
c.close();
return dbString;
}
This is the error I get from this particular code.
Process: com.example.vitaliy_2.emailpassworddatabasetrial, PID: 11254
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:5191) 
at android.view.View$PerformClick.run(View.java:20916) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5972) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Caused by: java.lang.StringIndexOutOfBoundsException: length=12; regionStart=4; regionLength=-1
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at com.example.vitaliy_2.emailpassworddatabasetrial.EmailDBHandler.phoneNumberDatabaseToString(EmailDBHandler.java:257)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.printDatabase(SignUpMainClass.java:183)
at com.example.vitaliy_2.emailpassworddatabasetrial.SignUpMainClass.registerAccount(SignUpMainClass.java:153)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270) 
at android.view.View.performClick(View.java:5191) 
at android.view.View$PerformClick.run(View.java:20916) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5972) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
Thanks for reading

You should read the method description carefully. String.substring (int start, int end) parameters are start and end, not start and length.
So using it as
holder.substring(4, 3) you try to take substring starting at 4th, and ending at 3rd char, the length of this substring naturally is -1

String index is 0 based, holder.substring( 1, 3) should be holder.substring(0, 3) etc. (The first parameter is inclusive and second parameter is exclusive to 0 - 3 means it will get index 0, 1 and 2)

Related

Android: Getting string from cursor following a raw query in SQLITE

I am trying to get a string back from a raw query to my SQLITE database.
The issue is that I am bridging two tables, so I'm not sure how to call the column which contains the color (the value needed).
How can I get the string value being returned from the SQL query to my result variable?
public String studentColor(){
SQLiteDatabase db = getWritableDatabase();
String queryStudent = "SELECT students.color FROM students, comments WHERE comments.author = students.name;";
Cursor cursor = db.rawQuery(queryStudent, null);
String result = // get students.color from cursor;
return result
}
Thanks
EDIT
ERROR
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.me.echoboard, PID: 21552
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389)
at android.view.View.performClick(View.java:6213)
at android.widget.TextView.performClick(TextView.java:11074)
at android.view.View$PerformClick.run(View.java:23645)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384)
at android.view.View.performClick(View.java:6213) 
at android.widget.TextView.performClick(TextView.java:11074) 
at android.view.View$PerformClick.run(View.java:23645) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6692) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.example.me.echoboard.DBHandler.studentColor(DBHandler.java:121)
at com.example.me.echoboard.login.onClick(login.java:37)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:384) 
at android.view.View.performClick(View.java:6213) 
at android.widget.TextView.performClick(TextView.java:11074) 
at android.view.View$PerformClick.run(View.java:23645) 
at android.os.Handler.handleCallback(Handler.java:751) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6692) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
You are not moving within the cursor so it's at position -1 (before the first row)
Use the following this moves to the first row in the cursor (if there is one, if not an empty string will be returned) :-
public String studentColor(){
SQLiteDatabase db = getWritableDatabase();
String result = "";
String queryStudent = "SELECT students.color FROM students, comments WHERE comments.author = students.name;";
Cursor cursor = db.rawQuery(queryStudent, null);
if (cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex("students.color"));
}
cursor.close();
return result
}
What you need to do is change your query with this and then cursor
"SELECT students.color as color FROM students, comments WHERE comments.author = students.name;";
Cursor
String result = cursor.getString(cursor.getColumnIndex("color"));

WHERE CLAUSE WITH MULTIPLE CONDITIONS NOT WORKING

On trying the following code i get the error:
Cursor cursor1 = db.rawQuery("SELECT * FROM " + TABLE_TRAVEL +
" WHERE " + KEY_CITY + " = ? AND " + KEY_STATE + " = ?",null);
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anvipuri.databaseplaces, PID: 11773
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.anvipuri.databaseplaces/com.anvipuri.databaseplaces.MainActivity}:
android.database.CursorIndexOutOfBoundsException: Index -1 requested,
with a size of 6
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1
requested, with a size of 6
at
android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at
android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at
android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at
com.anvipuri.databaseplaces.DatabaseHelper.findOnep(DatabaseHelper.java:132)
at
com.anvipuri.databaseplaces.MainActivity.onCreate(MainActivity.java:119)
at android.app.Activity.performCreate(Activity.java:6679)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
You should do
if(cursor.moveToFirst()) {
do {
Obj obj = createObjectFromCursor(cursor);
} while(cursor.moveToNext());
}
Or at least you shouldn't forget to call moveToPosition() or moveToFirst() so that you initialize the cursor's index from the default -1.

How to correctly Read Contacts information

I have been attempting to Read Contacts and just for the moment Log them to the console but I am getting a RunTime Exception error on my app. I have been following this tutorial https://www.youtube.com/watch?v=g4_1UOFNLEY&t=1s
I am not sure if this is the correct way of getting contact details and would appreciate any advice. My end goal would be to eventually have a contact picker where a user can choose a contact and add their name to a list
I have the permissions working correctly (as cleared up by another user in another thread) but still having problems.
Method called in onCreate:
private void viewContacts(){
// TODO CONTACT INFO - will log to console
ContentResolver resolver = getContentResolver();
// to gets all contacs
Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()){
// Access specific field of contact
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//get phone numbers
Cursor phoneCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{ contactId }, null);
Log.i("MY INFO", contactId + " = " +contactName);
//get phone num
while(phoneCursor.moveToNext()){
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("MY INFO", phoneNumber);
}
// get email details
Cursor emailCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",new String[]{ contactId}, null);
while(emailCursor.moveToNext()){
String contactEmail = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.i("MY INFO", contactEmail);
}
}
Error
/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{AppName.EditPlayersActivity}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.database.CursorWrapper.getString(CursorWrapper.java:142)
at AppName.EditPlayersActivity.viewContacts(EditPlayersActivity.java:175)
at AppName.EditPlayersActivity.permissionsCheck(EditPlayersActivity.java:222)
at AppName.EditPlayersActivity.onCreate(EditPlayersActivity.java:132)
at android.app.Activity.performCreate(Activity.java:6248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544) 
at android.app.ActivityThread.access$900(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5845) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
04-24 17:37:48.206 19943-19943/AppName.squad E/AndroidRuntime: FATAL EXCEPTION: main
Process: AppName.squad, PID: 19943
java.lang.RuntimeException: Unable to start activity ComponentInfo{AppName.squad/AppName.squad.EditPlayersActivity}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.database.CursorWrapper.getString(CursorWrapper.java:142)
at AppName.squad.EditPlayersActivity.viewContacts(EditPlayersActivity.java:175)
at AppName.squad.EditPlayersActivity.permissionsCheck(EditPlayersActivity.java:222)
at AppName.squad.EditPlayersActivity.onCreate(EditPlayersActivity.java:132)
at android.app.Activity.performCreate(Activity.java:6248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544) 
at android.app.ActivityThread.access$900(ActivityThread.java:150) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:168) 
at android.app.ActivityThread.main(ActivityThread.java:5845) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
The code you provided:
Cursor emailCursor =
resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{contactId},
null);
it seems that you are querying for email in the phone content provider. Shouldn't you use ContactsContract.CommonDataKinds.Email.CONTENT_URI instead?

No such column error in SQLITE database

I am getting error as NO SUCH COLUMN in logcat and it is inserting properly
How can I rectify this problem.Values which are given by me are representing correctly but SELECT query is not working for that values which are located in database table.
String search = "select * from "+ TABLE_NAME +" Where PHONENO =" +num+ "
and NAME = "+name1+" and LASTNAME =" +lastname1;
Cursor c = db.rawQuery(search,null);
boolean res = c.moveToFirst();
if (res) {
String s1 = c.getString(0);
String s2 = c.getString(1);
String s3 = c.getString(2);
String s4 = c.getString(3);
String s5 = c.getString(4);
String s6 = c.getString(5);
String s7 = c.getString(6);
String s8 = c.getString(7);
String s9 = c.getString(8);
String s10 = c.getString(9);
String s11 = c.getString(10);
String s12 = c.getString(11);
String s13 = c.getString(12);
String s14 = c.getString(13);
String s15 = c.getString(14);
String array[] ={s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13,s14,s15};
Intent i = new Intent(this,SelectMember.class);
i.putExtra("k1",array);
startActivity(i);
}
Logcat
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.database, PID: 9112
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: android.database.sqlite.SQLiteException: no such column: durga (code 1): , while compiling: select * from MASTER_ACCOUNTS Where PHONENO =8121680624 and NAME = durga and LASTNAME =veerepalli at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.example.user.database.Editoption.search(Editoption.java:36)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Application terminated.
String literals should be put in single quotes (')
Where PHONENO ='" +num+ "' and NAME = '"+name1+"' and LASTNAME ='" +lastname1 +"'";
By the way, this will blow up when the value contains a quote. Escape the string, or better use parameters.
Caused by: android.database.sqlite.SQLiteException: no such column:
durga (code 1): , while compiling: select * from MASTER_ACCOUNTS Where
PHONENO =8121680624 and NAME = durga and LASTNAME =veerepalli
Rectify Your Query first.
Informed you NO such table.
"SELECT * FROM "+ TABLE_NAME +" WHERE PHONENO ='" +num+ "' AND NAME = '"+ name1 +"' AND LASTNAME ='" + lastname1 +"'";
Then Uninstall old App and Clean-Rebuild-Run .

User Text in SQL Statement Cause App to Crash

I have an edittext, a button and a sqlite table. When the button is pressed a select statement runs that includes the text that was entered. The app works fine if the user only enters numbers, but crashes if it contains letters.
Here is the schema of the table:
public static abstract class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String STOP = "entryid";
public static final String LINE = "title";
public static final String SCHEDULE = "schedule";
}
The table is initialized. The onClick method is:
public void lookUp(View v) {
mEdit = (EditText) findViewById(R.id.text);
String string = mEdit.getText().toString();
SQLiteDatabase db = mDbHelper.getReadableDatabase();
String[] projection = {
FeedReaderContract.FeedEntry.STOP,
FeedReaderContract.FeedEntry.LINE,
FeedReaderContract.FeedEntry.SCHEDULE,
};
String sortOrder =
FeedReaderContract.FeedEntry.SCHEDULE + " ASC";
Cursor cursor = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME,
projection,
FeedReaderContract.FeedEntry.LINE+" = " + string ,
null,
null,
null,
sortOrder
);
Why does the app crashes when I have letters in the edittext? The columns are string types, so a string shouldn't crash it.
Here is the logcat error:
(1) no such column: line1
Shutting down VM
FATAL EXCEPTION: main
Process: com.mycompany.antibes, PID: 2767
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: android.database.sqlite.SQLiteException: no such column: line1 (code 1): , while compiling: SELECT entryid, title, schedule FROM entry WHERE title = line1 ORDER BY schedule ASC
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.mycompany.antibes.MainActivity.lookUp(MainActivity.java:61)
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at android.view.View$1.onClick(View.java:4015) 
at android.view.View.performClick(View.java:4780) 
at android.view.View$PerformClick.run(View.java:19866) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Your sql is
SELECT entryid, title, schedule FROM entry WHERE title = line1 ORDER BY schedule ASC
which means it's comparing title to a column instead of a value.
Try adding single quotes around your string.
Cursor cursor = db.query(
FeedReaderContract.FeedEntry.TABLE_NAME,
projection,
FeedReaderContract.FeedEntry.LINE+" = \'" + string + "\'" ,
null,
null,
null,
sortOrder
);
So you would get
SELECT entryid, title, schedule FROM entry WHERE title = 'line1' ORDER BY schedule ASC
which seems to be what you're trying to do.

Categories

Resources