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.
Related
public void Clicking(View view) {
double one = Double.parseDouble(testtt.getText().toString());
double two = Double.parseDouble(tessss.getText().toString());
}
already;
testtt [TextView] = "0.0001974794"
tessss [TextView] = "0"
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.Testing_Appi, PID: 2988
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NumberFormatException: For input string: "0.0001974794"
at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
at java.lang.Double.parseDouble(Double.java:547)
at com.example.Testing_Appi.MainActivity.testor(MainActivity.java:153)
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
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:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
I couldn't spot where the error was!
Example : Find your view by id with view
EditText testttET = view.findViewById(R.id.testtt);
String text = testttET.getText().toString();
check if your text is not empty
if(!text.equals("")){
double value = Double.parseDouble(text);
}
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"));
I have a problem when I try to delete elements on an arrayList. I need to put some elements on other array and then delete this elements from the original array. I put the code below:
private void setOrderAnswers(int position) {
for (int i = 0; i < 4; i++) {
listAnswersAux.add(listAnswers.get((position * 4) + i));
}
for (int i = 0; i < 4; i++) {
listAnswers.remove((position * 4) + i);
}
}
The error I have is an IndexOutOfBounds exception:
Caused by: java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
I don't know why I have this exception when I try to remove this index, but when I go to the index to copy the element, there is no problem. I mean, the index is OK because I can see the element before delete it.
And I always have the exception on Index value = 10, I can do all this operations before going to index=10 (position = 2).
Can anyone helps me? Thanks a lot!
06-14 09:58:58.455 31337-31337/com.prodintec.am_motion E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.prodintec.am_motion, PID: 31337
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prodintec.am_motion/com.prodintec.am_motion.QuizActivity}: java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
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: java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
at java.util.ArrayList.get(ArrayList.java:411)
at com.prodintec.am_motion.QuizActivity.setOrderAnswers(QuizActivity.java:201)
at com.prodintec.am_motion.QuizActivity.randomQuestions(QuizActivity.java:168)
at com.prodintec.am_motion.QuizActivity.onCreate(QuizActivity.java:48)
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)
try this
private void setOrderAnswers(int position) {
for (int i = 0; i < 4; i++) {
listAnswersAux.add(listAnswers.get((position * 4) + i));
listAnswers.remove((position * 4) + i);
}
}
I know this problem so many times asked. and i read every post that made by others here but i cant really solve this problem on my case.
Please help me why this happen and how to fix it ?
this is my code :
public StoreData getStore(String storeid) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select * from " + TABLE_NAME + " where "
+ COL_STORE_ID + "=?", new String[]{storeid});
StoreData data;
System.out.println("This is the problem");
c.moveToFirst();
data = new StoreData(
c.getString(c.getColumnIndex(COL_STORE_ID)),
c.getString(c.getColumnIndex(COL_ACCOUNT_ID)),
c.getString(c.getColumnIndex(COL_KOTA_ID)),
c.getString(c.getColumnIndex(COL_STORE_NAME)),
c.getString(c.getColumnIndex(COL_STORE_MANAGER)),
c.getString(c.getColumnIndex(COL_STORE_ADDRESS)),
c.getString(c.getColumnIndex(COL_STORE_TELEPHONE)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LAT)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LONG)),
c.getString(c.getColumnIndex(COL_STORE_LEADTIME)),
c.getString(c.getColumnIndex(COL_STORE_MD)));
db.close();
c.close();
return data;
}
this is my log :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.apps.userinarts.mobilemerchpre/com.apps.userinarts.MobileMerchPre.StoresCheckinActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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.CursorIndexOutOfBoundsException: Index 0 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.apps.userinarts.MobileMerchPre.db.StoreHelper.getStore(StoreHelper.java:130)
at com.apps.userinarts.MobileMerchPre.StoresCheckinActivity.inCaseOffline(StoresCheckinActivity.java:90)
at com.apps.userinarts.MobileMerchPre.StoresCheckinActivity.onCreate(StoresCheckinActivity.java:83)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
looks like your query returned 0 results. To check it, don't ignore result of moveToFirst(). It will return false if cursor size is 0.
Do something like this:
if(c.moveToFirst()) {
data = new StoreData(
c.getString(c.getColumnIndex(COL_STORE_ID)),
c.getString(c.getColumnIndex(COL_ACCOUNT_ID)),
c.getString(c.getColumnIndex(COL_KOTA_ID)),
c.getString(c.getColumnIndex(COL_STORE_NAME)),
c.getString(c.getColumnIndex(COL_STORE_MANAGER)),
c.getString(c.getColumnIndex(COL_STORE_ADDRESS)),
c.getString(c.getColumnIndex(COL_STORE_TELEPHONE)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LAT)),
c.getString(c.getColumnIndex(COL_STORE_GEO_LONG)),
c.getString(c.getColumnIndex(COL_STORE_LEADTIME)),
c.getString(c.getColumnIndex(COL_STORE_MD)));
db.close();
c.close();
}
return data;
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)