【问题标题】:NullPointerException in Loop循环中的 NullPointerException
【发布时间】:2014-11-25 03:43:34
【问题描述】:

我创建了一个程序,它尝试使用字典破解受密码保护的 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

【问题讨论】:

  • 你为什么打电话给passwordArray.get(0),你为什么要投给String?
  • 就此而言,您为什么要在 (line = br.readLine()) != null 上进行测试,并承认它可以变成 null,但在该条件内没有所有应该触发的代码?
  • 字典里是一个该死的空白行...pastebin.com/UMRBPTnB

标签: java loops arraylist nullpointerexception


【解决方案1】:

先查字典……

gorillaz
foxylady
darkside
angie1
321456

summer05
sabrina1
rosalinda
roderick
muslim
matilde
indigo

“简单到让人心疼。”

【讨论】:

    【解决方案2】:

    在整个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;
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2014-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      相关资源
      最近更新 更多