如何清除java.lang.IndexOutOfBoundsException?

时间:2016-01-29 12:18:46

标签: java android arrays

这是我遇到java.lang.IndexOutOfBoundsException错误的方法。当我调试代码并看到时,我就是这样的

  

tempkoradel [j] = java.lang.IndexOutOfBoundsException:无效的数组范围:5到5   columnTags [j] = java.lang.IndexOutOfBoundsException:无效的数组范围:5到5。

        try {
            SoapObject request = new SoapObject(NAMESPACE, "GetAssignDetails");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
          //  request.addProperty("barcode",params[0]);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(NAMESPACE + "GetAssignDetails", envelope);
            } catch (IOException | XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            SoapPrimitive response;
            String Koradcdetailss = null;
            try {
                response = (SoapPrimitive) envelope.getResponse();
                Koradcdetailss = response.toString();

                if (Koradcdetailss != null) {
                    String[] columnTags = new String[]{"StyleNo", "Color", "Size", "BarcodeNo", "NoOfPcs"};

                    int[] columnIds = new int[]{R.id.txt_styleno, R.id.txt_color, R.id.txt_size, R.id.txt_barcodeno, R.id.txt_pcs};

                    String[] Koradcdeldetails = Koradcdetailss.split(":,");
                    if (Koradcdeldetails.length > 0) {
                        list = new ArrayList<HashMap<String, String>>();
                        for (int i = 0; i < Koradcdeldetails.length; i++) {
                            if (!Koradcdeldetails[i].isEmpty()) {
                                String[] tempkoradel = Koradcdeldetails[i].split(";");
                                HashMap<String, String> map = new HashMap<String, String>();
                                //initialize row data
                                for (int j = 0; j < (columnTags.length); j++) {
                                    map.put(columnTags[j], tempkoradel[j]);
                                }

                                list.add(map);
                            }
                        }
                        listhdr = new ArrayList<HashMap<String, String>>();
                        HashMap<String, String> map = new HashMap<String, String>();
                        //initialize row data
                        for (int j = 0; j < columnTags.length; j++) {
                            map.put(columnTags[j], columnTags[j]);
                        }
                        listhdr.add(map);
                        SimpleAdapter arrayAdapterhdr =
                                new SimpleAdapter(RFIDscanningActivity.this, listhdr, R.layout.rfid_scanning_row,
                                        columnTags, columnIds);
                        listViewhdr.setAdapter(arrayAdapterhdr);

                        SimpleAdapter arrayAdapter =
                                new SimpleAdapter(RFIDscanningActivity.this, list, R.layout.rfid_scanning_row,
                                        columnTags, columnIds);
                        listView.setAdapter(arrayAdapter);

                    }
                } else {
                    //Helper.InfoMsg("Info", "No Stock Found", this);

                }
            } catch (Exception e) {
                //Helper.InfoMsg("Info", "No Stock Found1", this);

            }
        } catch (Exception er) {
            //Helper.warning("Error", "Error 2" + er.toString(), this);
        }
        return null;
    }

请帮我解决上述错误。

2 个答案:

答案 0 :(得分:1)

 list = new ArrayList<HashMap<String, String>>();
                        for (int i = 0; i < Koradcdeldetails.length; i++) {
                            if (!Koradcdeldetails[i].isEmpty()) {
                                String[] tempkoradel = Koradcdeldetails[i].split(";");
                                HashMap<String, String> map = new HashMap<String, String>();
                                //initialize row data
                                for (int j = 0; j < (tempkoradel.length); j++) {
                                    map.put(columnTags[j], tempkoradel[j]);
                                }

                                list.add(map);
                            }

如果数组中有3个值,并且您尝试访问实际上不存在的第四个元素,则会得到IndexOutOfBoundsException。所以在处理数组时需要小心。

答案 1 :(得分:0)

Just Go With length() - 1

for (int j = 0; j < (columnTags.length - 1); j++) {
                                map.put(columnTags[j], tempkoradel[j]);
                            }