我已经实现了搜索功能以获取客户的详细信息,但是当我选择搜索的项目时,它给我一个错误
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
依此类推
这是我使用Json搜索客户详细信息并存储在数组中的代码,但是当我搜索数据并选择它时,出现上述错误。请帮帮我。谢谢
public void RunSearchClientService() {
//progressDialog.show();
JsonObjectRequest postRequest = new JsonObjectRequest
(Request.Method.POST, Network.API_URL + "clients/search", api_parameter, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject result = ((JSONObject) response.get("data"));
JSONArray clients = (JSONArray) result.get("clients");
JSONArray invoice_lines = (JSONArray) result.get("invoice_lines");
Integer invoice_number = helper_string.optInt(result, "invoice_number");
logoImage = helper_string.optString(result, "logo");
if (invoice_number > 0) {
edit_invoice_number.setText(String.format("%04d", invoice_number));
toolbar.setTitle(String.format("INV-%04d", invoice_number));
}
array_list_clients.clear();
array_clients = new String[clients.length()];
Integer selected_client_index = 0;
if (clients.length() > 0) {
for (int i = 0; i < clients.length(); i++) {
JSONObject obj = clients.getJSONObject(i);
Client client = new Client();
client.Id = obj.optInt("id");
client.UserId = obj.optInt("user_id");
client.Name = helper_string.optString(obj, "name");
client.Reg_Num = obj.optString("reg_num");
client.Email = helper_string.optString(obj, "email");
client.Address1 = helper_string.optString(obj, "address1");
client.Address2 = helper_string.optString(obj, "address2");
client.City = helper_string.optString(obj, "city");
client.State = helper_string.optString(obj, "state");
client.Postcode = helper_string.optString(obj, "postcode");
client.Country = helper_string.optString(obj, "country");
array_list_clients.add(client);
array_clients[i] = client.Name + " " + "Reg No. : "+ client.Reg_Num ;
if (currentInvoice != null && currentInvoice.ClientId == client.Id) {
selected_client_index = i;
currentClient = client;
}
/*if (obj.optInt("invoice_number") > 0)
invoice_number = obj.optInt("invoice_number");*/
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(NewInvoiceActivity.this, R.layout.custom_simple_spinner_item, array_clients);
spinner_client.setAdapter(adapter);
spinner_client.setSelection(selected_client_index);
}
if (invoice_lines.length() > 0) {
for (int i = 0; i < invoice_lines.length(); i++) {
JSONObject obj = invoice_lines.getJSONObject(i);
Item item = new Item();
item.Id = obj.optInt("id");
item.Quantity = obj.optInt("quantity");
item.Name = helper_string.optString(obj, "name");
item.Rate = obj.optDouble("rate");
item.Description = helper_string.optString(obj, "description");
array_list_items.add(item);
}
calculate_total();
setListViewHeightBasedOnChildren(list_items);
}
if (array_list_items_from_intent != null && array_list_items_from_intent.size() > 0) {
for (int i = 0; i < array_list_items_from_intent.size(); i++) {
array_list_items.add(array_list_items_from_intent.get(i));
}
calculate_total();
setListViewHeightBasedOnChildren(list_items);
}
} catch (Exception ex) {
Toast.makeText(NewInvoiceActivity.this, R.string.error_try_again_support, Toast.LENGTH_LONG).show();
}
// if (progressDialog != null && progressDialog.isShowing()) {
// // If the response is JSONObject instead of expected JSONArray
// progressDialog.dismiss();
// }
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
if (progressDialog != null && progressDialog.isShowing()) {
// If the response is JSONObject instead of expected JSONArray
progressDialog.dismiss();
}
NetworkResponse response = error.networkResponse;
if (response != null && response.data != null) {
try {
JSONObject json = new JSONObject(new String(response.data));
// Toast.makeText(NewInvoiceActivity.this, json.has("message") ? json.getString("message") : json.getString("error"), Toast.LENGTH_LONG).show();
} catch (JSONException ex) {
Toast.makeText(NewInvoiceActivity.this, R.string.error_try_again_support, Toast.LENGTH_SHORT).show();
}
} else {
// Toast.makeText(NewInvoiceActivity.this, error != null && error.getMessage() != null ? error.getMessage() : error.toString(), Toast.LENGTH_LONG).show();
}
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("X-API-KEY", MainActivity.api_key);
return params;
}
};
// Get a RequestQueue
RequestQueue queue = MySingleton.getInstance(NewInvoiceActivity.this).getRequestQueue();
//Used to mark the request, so we can cancel it on our onStop method
postRequest.setTag(TAG);
MySingleton.getInstance(NewInvoiceActivity.this).addToRequestQueue(postRequest);
}
答案 0 :(得分:0)
错误ArrayIndexOutOfBoundsException:length = 1; index = 1表示索引1处的数组无效;换句话说,您正在尝试访问只有一个元素的数组的第二个元素。
下面的代码将给出类似的错误:
public class ReplicateError {
public static void main(String args[]) {
// reproducing java.lang.ArrayIndexOutOfBoundsException : 1 error
String[] clients = {"John"};
String client = clients[1];
// this will throw java.lang.ArrayIndexOutOfBoundsException : 1
System.out.println(client);
}
}
我建议您进行装订检查:
if (args.length < 2) {
System.err.println("Not enough arguments received.");
return;
}