解析数据时出错java.lang.ArrayIndexOutOfBoundsException

时间:2013-02-12 16:24:10

标签: android arrays android-listview

在循环中,' m'从0到4工作但随后崩溃,而不是从0到6。

ArrayList<ItemTeacher> results = new ArrayList<ItemTeacher>();
String resultteacher="";
String[] binku2=null;

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
    HttpClient httpclient = new DefaultHttpClient();

    HttpPost httppost = new HttpPost("http://"+localip+":8080/Apj/Teacher_Servlet");

    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
         sb.append(line);
    }
    is.close();
    resultteacher = sb.toString();
    System.out.println("RESULT is : -**********"+resultteacher+"********-");


    String[] binku = resultteacher.split("@");

    System.out.println("LENGTH OF BINKU = "+binku.length);

    for(int m=0;m<=binku.length-1;m++)
    {

        binku2 = binku[m].split("#");
        System.out.println("Record number"+binku2[m]);
        drawable = LoadImageFromWebOperations("http://"+localip+":8080/Apj/TeacherImage/"+binku2[4]+".png");
        System.out.println("IMAGE DATA DISPLAY ="+drawable);

        ItemTeacher item_teacher = new ItemTeacher();

        item_teacher.setname(binku2[0]);
            item_teacher.setinfo(binku2[1]);
            item_teacher.setimage(drawable);
            String binku3[] = binku2[2].split("~");
            String binku4[] = binku2[3].split("~");

            String time="";
            String timeroom="";
            time =("Lecture Timings - Classroom");
            for(int i=0;i<=8;i++)
            {
            //  time +=("[Timings and Room]");
                timeroom +=("["+binku3[i]+" - "+binku4[i]+"]"+"      ");
                //System.out.println("Record number"+binku3[i]);
            }
            item_teacher.settime(time);
            item_teacher.setroom(timeroom);
            System.out.println("m value is ="+m);

            results.add(item_teacher);

    }
}


catch (Exception e) {
     System.out.println("Error parsing data " + e.toString());
}

return results;
}

Logcat详细信息:

02-12 21:33:36.772: I/System.out(358): RESULT is : -**********Mrs Roopali Sood#M.C.M (Head IT Dept)#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#F8~Lab 1~Lab 3~F7~F8~S9~Free~S8~F8~#1@Mr Jagmohan Mago#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Free~S2~F8~S1~F9~Free~Lab 2~S9~Lab1~#2@Mr Munish Gupta#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#S9~F8~S1~Lab 1~F1~Free~F8~Lab 3~S5~#3@Mrs Pallavi Chopra#M.C.A., M.Phil. (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Lab 2~Free~S2~F8~Free~Free~Lab 2~Free~Lab1~#4@Mr Abhejeet Singh#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Lab 3~Lab 1~Lab 3~S7~F8~Lab 2~Free~S8~S9~#5@Vinay#M.C.A (IT Dept).#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#Free~S2~F8~S1~F9~Free~Lab 2~S9~Lab1~#6@Bharti#Pata nai#9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~#S9~F8~S1~Lab 1~F1~Free~F8~Lab 3~S5~#7@********-
02-12 21:33:36.784: I/System.out(358): LENGTH OF BINKU = 7
02-12 21:33:36.784: I/System.out(358): Record numberMrs Roopali Sood
02-12 21:33:37.082: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@4618b990
02-12 21:33:37.102: I/System.out(358): m value is =0
02-12 21:33:37.102: I/System.out(358): Record numberM.C.A (IT Dept).
02-12 21:33:37.512: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@4619c2a8
02-12 21:33:37.522: I/System.out(358): m value is =1
02-12 21:33:37.522: I/System.out(358): Record number9:00~9:45~10:30~10:45~11:30~12:15~1:00~1:45~3:30~
02-12 21:33:37.772: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461ac610
02-12 21:33:37.782: I/System.out(358): m value is =2
02-12 21:33:37.792: I/System.out(358): Record numberLab 2~Free~S2~F8~Free~Free~Lab 2~Free~Lab1~
02-12 21:33:38.102: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461bca98
02-12 21:33:38.112: I/System.out(358): m value is =3
02-12 21:33:38.112: I/System.out(358): Record number5
02-12 21:33:38.492: I/System.out(358): IMAGE DATA DISPLAY =android.graphics.drawable.BitmapDrawable@461ccc80
02-12 21:33:38.492: I/System.out(358): m value is =4
02-12 21:33:38.505: I/System.out(358): Error parsing data java.lang.ArrayIndexOutOfBoundsException

2 个答案:

答案 0 :(得分:2)

这几乎肯定不是你想要的:

binku2 = binku[m].split("#");
System.out.println("Record number"+binku2[m]);

您似乎在任何地方操作,就好像binku2数组的长度为4,但之后您尝试访问值为m的索引。因此,当您进入循环的第5次迭代时,m为4并且您抛出异常。可能你打算在这里访问binku

binku2 = binku[m].split("#");
System.out.println("Record number"+binku[m]);

答案 1 :(得分:1)

通过像这样对循环进行硬编码,你正在为自己设置错误:

for(int i=0;i<=8;i++)

您应始终使用动态值,例如m<=binku.length-1或更简单的版本m < binku.length。当您使用多个阵列时,最安全的方法是检查每个阵列的长度:

for(int i = 0; i < binku3.length && i < binku4.length; i++)