2013-01-15 81 views
0

我一直在一塊工作,當我嘗試運行它,我的IDE凍結和我得到使用任務管理器來關閉它。我的Java代碼導致的NetBeans凍結

有什麼想法爲什麼? 我知道它是與迭代器和while循環我使用

有誰能夠發現了嗎? 下面的servlet代碼

try { 
     List<FileItem> fields = upload.parseRequest(request); 

     Iterator<FileItem> it = fields.iterator(); 




     //starting conversion from file to sample 


     //creating a container to hold the samples 
     ArrayList<sample> samplelist = new ArrayList(); 

     while (it.hasNext()) { 

      sample c = new sample(); 
      FileItem fileItem = it.next(); 

      c.setFileName(fileItem.getName()); 
      c.setPayload(fileItem.getString()); 
      samplelist.add(c); 
     } 

     //looping through uploaded samples to split code into lines 
     for (int i = 0; i < samplelist.size(); i++) { 
      sample current = new sample(); 
      current = samplelist.get(i); 

      //splitting whole code block into lines based on ; 
      String[] temp; 
      temp = current.getPayload().split(";"); 

      //going through split lines and adding them to the sample's rawcode 
      //arraylist after checking for for loops 
      for (int j = 0; j < temp.length; j++) { 

       // current.Raw_Code.add(temp[j]); 
       String current_line = temp[j]; 




       current.Raw_Code.add(current_line); 

      } 




     } 

//testing 
     System.out.print("starting testing"); 
     for (int i = 0; i < samplelist.size(); i++) { 
      sample current = new sample(); 
      current = samplelist.get(i); 
      System.out.print("File name <br/>"); 
      current.getFileName(); 
      System.out.print("lines detected <br/>"); 

      Iterator itr = current.Raw_Code.iterator(); 

      //this while loop started to cause the problem 

      while(itr.hasNext()){ 
      System.out.print("x"); 
      } 
     } 




    } catch (FileUploadException e) { 
     e.printStackTrace(); 
    } 



    out.close(); 
    return; 
+5

使用分步調試 –

回答

10

就在這裏的一部分:

while(itr.hasNext()){ 
    System.out.print("x"); 
} 

你是不是叫itr.next()所以迭代器不是迭代。 :)在您的while循環內部添加itr.next()將糾正問題。

+2

這是其中的一個:」讓CPU燒「的問題:) –

+0

哦thankss:d – kamil

1

你同時是一個無限循環 - 迭代器總是hasNext因爲它從來沒有迭代...所以它只是不斷去。

在另一方面,每當IDE崩潰,你應該想到。「無限循環,因爲它是規劃問題的一個常見的副作用