I am trying to get data from the db in the response object and showing it on the client side. But I am getting this array index out of bounds exception.I am sure I am doing it correct,but just cant make it run.
the error I get is
java.lang.ArrayIndexOutOfBoundsException: length=1956; index=1956
at com.pda.kaizen.ConnectionImpl.getCustomers(ConnectionImpl.java:139)
at com.pda.kaizen.activity.MainMenuActivity$1.run(MainMenuActivity.java:123)
at java.lang.Thread.run(Thread.java:856)
this is the server side code
StringBuilder sb = new StringBuilder();
sb.append("100|OK");
for (Customer customer : customers) {
sb.append("|").append(customer.getId())
.append("|").append(customer.getCode())
.append("|").append(customer.getName())
.append("|").append(customer.getNameFarsi())
.append("|").append(customer.getAddress())
.append("|").append(customer.getType())
.append("|").append(customer.getPhoneNo())
.append("|").append(customer.getMobileNo())
.append("|").append(customer.getFaxNo())
.append("|").append(customer.getEmail())
.append("|").append(customer.getRegisterCode())
.append("|").append(customer.getOrganizationName())
.append("|").append(customer.getEconomicCode())
.append("|").append(customer.getMelliCode())
.append("|").append("-1".equals(customer.getPostCode())?"":customer.getPostCode())
.append("|").append(customer.getFirstName())
.append("|").append(customer.getLastName());
}
return sb.toString();
This is the Activity code
@Override
public List<Customer> getCustomers() {
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("action", "customer"));
try {
String response = executeHttpPost(params);
String[] result = response.split(DELIMITER);
List<Customer> customers = new ArrayList<Customer>();
for (int i = 2; i < result.length - 16; i += 17) {
String id = result[i + 0];
String code = result[i + 1];
//String name = result[i + 2];
String name = result[i + 3];
String address = result[i + 4];
String type = result[i + 5];
String phone = result[i + 6];
String mobile = result[i + 7];
String fax = result[i+8];
String email = result[i+9];
String registerCode = result[i+10];
String organizationName = result[i+11];
String economicCode = result[i+12];
String melliCode = result[i+13];
String postCode = result[i+14];
String firstName = result[i+15];
String lastName = result[i+16];
Customer customer = new Customer(id, code, name, address, type,phone,mobile,fax,email,registerCode,organizationName,economicCode,melliCode,postCode,firstName,lastName);
customers.add(customer);
}
return customers;
}
catch (Exception exc) {
Log.e("----", exc.getMessage(), exc);
throw new ConnectionException(exc.getMessage());
}
}
I may be wrong – but I believe your problem is with how you
splityour string.If you do this:
"a|b|c".split("\\|")– you will get an array of 3 strings.If you do this:
"a||".split("\\|")– you will get an array of 1 string.This is because
splittrims out the end.Assuming you are using
split– check your data and see if the last customer has a last name.If no – then I am correct.
See the following ideone code as a sample:
Example of code failing due to empty data
To solve the problem, you should introduce spaces between delimiters (or any other value you wish) to ensure
splittakes them into account.See the following ideone code as an example:
Example of code running with spaces between delimiters.
NOTE: Quote from the javadoc