1

I'm looping a buffered reader to pull lines from a dictionary, add them to an ArrayList, and then run them later in the program. Throughout this 15,000,000 line dictionary, blank lines come up about once every 30,000 lines. I don't expect all users of this program to use the dictionary I include, and I don't want to manually pick out and erase each blank line. When the program encounters this line, it throws a pesky NullPointerException, killing the program immediately. I've tried adding a try/catch to add the line if it's not blank and pass over a line that is null using continue. As always, here's my code:

                String line;
                if ((line = br.readLine()) != null) {
                    try{
                        passwordArray.add(line);
                        System.out.println(line);
                    } catch(NullPointerException npe){
                        System.out.println("The line is null.");
                        continue;
                    }
                }

Stack Trace:

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

Line 96:

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

Looking at line 96, the blank line is being added and cannot run because the library I'm using requires a password that's length is greater than 0. Line 55 is just a call to the method where this entire process is completed.

Zeki
  • 5,107
  • 1
  • 20
  • 27
Zulfe
  • 820
  • 7
  • 25
  • 1
    What is throwing the NPE — is it `passwordArray.add(line)`? Can you post a stack trace? `br.readLine()` should be returning an empty string, not a null, if the line is empty. Check that `!line.isEmpty()` before sending it on. – Reinstate Monica -- notmaynard Nov 25 '14 at 21:35
  • Stack trace added. Looking at line 96, the blank line is being added and cannot run because the library I'm using requires a password that's length is greater than 0. – Zulfe Nov 25 '14 at 21:39
  • So that's the problem, evidently. It's generally necessary to post the error message/stack trace you're getting and the lines in the code it refers to. Strange that that library would be throwing an NPE because it can't handle an empty string (which is certanly not null). – Reinstate Monica -- notmaynard Nov 25 '14 at 21:45
  • I'm not sure this is a duplicate of that question. The title here needs to be changed, but the actual problem is that `net.lingala.zip4j.core.ZipFile.setPassword()` throws an NPE in certain situations in which the argument is not null. – Reinstate Monica -- notmaynard Nov 25 '14 at 21:51
  • I'm fine with reopening it, but the [source code](http://grepcode.com/file/repo1.maven.org/maven2/net.lingala.zip4j/zip4j/1.2.3/net/lingala/zip4j/core/ZipFile.java#ZipFile.setPassword%28java.lang.String%29) suggests that the object isn't in the correct state. – Makoto Nov 25 '14 at 21:56
  • Maybe the line is blank but not null? Try adding && line.length()>0 to your conditional. Also if you want to catch the exception, your try catch needs to be around line 96. – Zeki Nov 25 '14 at 22:00
  • That might not be the version OP is using — the linked listing for `setPassword()` doesn't throw a NullPointerException, which is what OP is getting in the stack trace. – Reinstate Monica -- notmaynard Nov 25 '14 at 22:01
  • I actually found the issue. The dictionary was giving a line of 8 spaces as a "password," which it was clearly not. I'm not even sure you can use spaces as passwords for locked zip files. I added the conditional `if(line.startsWith(" "))` to filter out blank or useless lines. – Zulfe Nov 25 '14 at 22:04
  • I don't know if spaces are allowed for zip passwords; if they are, then that might disqualify valid passwords. The library you're using uses `if (str == null || str.trim().length() <= 0) ...` which would be a reasonable check for you to use as well in that case. (See http://grepcode.com/file/repo1.maven.org/maven2/net.lingala.zip4j/zip4j/1.3.2/net/lingala/zip4j/core/ZipFile.java#ZipFile.setPassword%28java.lang.String%29) – Reinstate Monica -- notmaynard Nov 25 '14 at 22:08

1 Answers1

2

The problem is probably that your line is empty and not null. Here is the code for set password:

648     public void setPassword(String password) throws ZipException {
649         if (!Zip4jUtil.isStringNotNullAndNotEmpty(password)) {
650             throw new NullPointerException();
651         }
652         setPassword(password.toCharArray());
653     }

You need to change:

if ((line = br.readLine()) != null) {

to:

line = br.readLine();
if (line != null && line.length() > 0) {
Zeki
  • 5,107
  • 1
  • 20
  • 27