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