Home > Software design >  App crashes when adding to an ArrayList in a for loop after first succeeful add
App crashes when adding to an ArrayList in a for loop after first succeeful add

Time:10-03

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.

public void SetEkmFieldValueStrings(String A, String B)
  {
  // example string A content
  /* $00030000019412663312012914320863457705827487016831160515448803478652000007590515438109551618066451002454245424540005600000000300000984000000000007720001756 100C000 100000003800000000000056000009450020000001600000000000000000232002210030208144700!*/

  /* if length of A is less than 250 chars, read is no good, discard
  *  NOTE:  both Strings A and B will need to be validated for checksum
  *  TODO...
  * */
  if(A.length() < 250) {return;}

   // discard first 4 chars from string, not needed
   A = A.substring(4, A.length()-4);

   /* array of fields values are NOT empty */
   /* clear arrays for new sets of values */
  if(fieldsA.size() > 0)
    fieldsA.clear();
  if(fieldsB.size() > 0)
    fieldsB.clear();;

  int _start = 0; /* 1st index into field size array */
  int i;
  for(i = 0; i < _a.length; i  )
    {
    /*  add the substring of String A to the array list of field values
    *   _a[i] contains the length of string to extract from A
    * */
    fieldsA.add((String) A.substring(_start, _a[i]));
    String j = fieldsA.get(_start);  //for testing purpose only
    /* set _start to the next field start pont in string A */
    _start  = _a[i];
    }

    /* do same for String B*/
    _start = 0;
    for(i = 0; i < _b.length; i  )
      {
      /*  add the substring of String A to the array list of field values
       *   _b[i] contains the length in of string to extract
       * */
      fieldsB.add(B.substring(_start, _b[i]));
      /* set _start to the next field start pont in string A */
      _start  = _b[i];
      }

  }

CodePudding user response:

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 :

public String substring(int beginIndex, int endIndex)

beginIndex - the beginning index, inclusive.

endIndex - the ending index, exclusive.

  • Related