循环中的空指针异常



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

先查字典...

gorillaz
foxylady
darkside
angie1
321456
summer05
sabrina1
rosalinda
roderick
muslim
matilde
indigo

"这么简单,好痛。"

使这个条件到整个 while 循环,当该行为空时,它不会添加 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;
            }
        }
    }

最新更新