Skip to content
Advertisement

App crashes when adding to an ArrayList in a for loop after first succeeful add

Still new to android and java but getting there, here is my problem.

The crash occurs at this point fieldsA.add( A.substring(_start, _a[i])); it works the first time but not the second time, I do not get an error in my debug window, just a crash.

// number of chars to extract from string value to add to ArrayList.

int _a[] = {12,8,8,8,24,24,8,8,4,4,4,5,5,5,7,7,7,7,4,4,4,7,7,7,7,4,8,8,8,1,1,1,1,2};

int _b[] = {2,32,32,4,4,4,5,5,5,7,7,7,7,4,4,4,8,1,4,4,4,4,1,4,56,14 };

for clarity, _start value is correct and length of string to extract (_a[i]) is also correct, in the case of this crash, _start value is 12 and _a[i] is 8 so the substring(12, 8) should add “12663312” to the ArrayList.

Question is, what am I doing wrong in how I am using array list, if I was using a vector (which I would normally do in C++) it would be a breeze but this is my first time use of ArratList in java and I cant see what the problem is, maybe I am too old.

Thanks in advance.

JavaScript

Advertisement

Answer

Looks to me that You use wrong indexes for substring.

Substring method second parameter is not count of chars to take but endIndex.

We can find relevant info in javadoc of String.substring :

JavaScript

beginIndex – the beginning index, inclusive.

endIndex – the ending index, exclusive.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement