2013-02-12 94 views
0

在循环中, 'M' 为0至4的工作原理,但然后崩溃,而不是从0到6错误解析数据java.lang.ArrayIndexOutOfBoundsException

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~#[email protected] 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~#[email protected] 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~#[email protected] 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~#[email protected] 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~#[email protected]#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~#[email protected]#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~#[email protected]********- 
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 [email protected]990 
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 [email protected] 
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 [email protected] 
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 [email protected] 
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 [email protected] 
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 

回答

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++) 
+0

它实际上是从'binku3'和'binku4'在那一点上读取的。 – eldarerathis 2013-02-12 16:29:45

+0

binku的长度是7 ie 0到6(7-1) 它的工作范围从0到4只有 – 2013-02-12 16:30:21

+0

@eldarerathis的确,我认为有很多错误可以找到,我会修改我的答案但留下它作为一般建议。 – Sam 2013-02-12 16:32:25

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]); 
+0

是可能出现循环,但我不知道如何?来自数据库的 有7个值到来,它只显示5个,即0到4个 – 2013-02-12 16:32:26

+0

'binku'中有7个值,而不是'binku2'。重新阅读我的文章:它看起来像你正在访问该行的错误数组*。 – eldarerathis 2013-02-12 16:33:39

+0

没有binku2为劈裂binku 的值,但我访问binku用于运行从0环路7-1 但它正在运行0-4 数据 太太Roopali索德#MCM(头IT部门)#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 @ – 2013-02-12 16:37:48