2014-11-25 86 views
-1

我創建了一個程序,試圖使用字典破解受密碼保護的zip文件。該程序從一行讀取,將該行存儲在位置爲0的ArrayList中,然後嘗試用它打開鎖定的zip文件。如果成功,則關閉緩衝讀取器並公佈工作密碼。如果不成功,它將忽略生成的ZipException,清除ArrayList中位置0的密碼,並繼續下一行。我已經包括調試,以顯示每次嘗試後ArrayList中的項目數量(應始終爲1)。循環中的空指針異常

代碼段(錯誤):

Exception in thread "main" java.lang.NullPointerException 
    at net.lingala.zip4j.core.ZipFile.setPassword(ZipFile.java:650) 
    at zZipCracker.zZipCracker.zZipCracker(zZipCracker.java:87) 
    at zZipCracker.zZipCracker.main(zZipCracker.java:55) 

代碼段(我的循環):

while(true) { //while elements are still in the array list 
        String line; 
        if ((line = br.readLine()) != null) { 
         passwordArray.add(line); 
         System.out.println(line); 
        } 
        zipper.setPassword((String) passwordArray.get(0)); //set the password to element position [passwordCounter] 
        System.out.println("Testing password no." + passwordCounter + ", which is " + passwordArray.get(0)); 
        passwordCounter = passwordCounter + 1; 

        try { 
         zipper.extractAll(dest); 
         br.close(); 
         JOptionPane.showMessageDialog(null, "The zip has been cracked. The password is " + passwordArray.get(0)); 
         break; 
        } catch(ZipException ze) { 
         System.out.println(passwordArray.size()); 
         passwordArray.remove(0); 
         continue; 
        } 
       } 
     } else { 
       zipper.extractAll(dest); 
       JOptionPane.showMessageDialog(null, "The selected zip was not password protected. It was extracted anyways."); 
     } 

代碼段(只需87行):

zipper.setPassword((String) passwordArray.get(0)); 

代碼段(輸出在例外之前):

darkside 
Testing password no.4747, which is darkside 
1 
angie1 
Testing password no.4748, which is angie1 
1 
321456 
Testing password no.4749, which is 321456 
1 

Exception in thread "main" java.lang.NullPointerException 
+2

你爲什麼要調用'passwordArray.get(0)',爲什麼要轉換爲'String'? – 2014-11-25 03:46:13

+2

爲什麼你測試'(line = br.readLine())!= null',確認它可以成爲'null',但是沒有在該條件內應該觸發的所有代碼都不爲null ? – 2014-11-25 03:47:13

+0

這是字典中該死的空行...... http://pastebin.com/UMRBPTnB – Zulfe 2014-11-25 03:50:32

回答

0

將此條件設置爲整個while循環,當該行爲null時,它不會添加passwordArray。但是下一行你試圖從passwordArray.get(0)獲取值。所以你必須爲整個代碼做出條件。

while (true) 
    { //while elements are still in the array list 
     String line; 
     if ((line = br.readLine()) != null) 
     { 
      passwordArray.add(line); 
      System.out.println(line); 

      zipper.setPassword((String) passwordArray.get(0)); //set the password to element position [passwordCounter] 
      System.out.println("Testing password no." + passwordCounter + ", which is " + passwordArray.get(0)); 
      passwordCounter = passwordCounter + 1; 

      try 
      { 
       zipper.extractAll(dest); 
       br.close(); 
       JOptionPane.showMessageDialog(null, "The zip has been cracked. The password is " + passwordArray.get(0)); 
       break; 
      } 
      catch (ZipException ze) 
      { 
       System.out.println(passwordArray.size()); 
       passwordArray.remove(0); 
       continue; 
      } 
     } 
    } 
0

查字典第一...

gorillaz 
foxylady 
darkside 
angie1 
321456 

summer05 
sabrina1 
rosalinda 
roderick 
muslim 
matilde 
indigo 

「那麼簡單,它傷害。」