循环中的NullPointerException

时间:2014-11-25 03:43:34

标签: java loops arraylist nullpointerexception

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

答案 0 :(得分: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;
            }
        }
    }

答案 1 :(得分:0)

首先检查词典......

gorillaz
foxylady
darkside
angie1
321456

summer05
sabrina1
rosalinda
roderick
muslim
matilde
indigo

"这很简单就会伤害。"