0

I am trying to read the names of some xml files from a folder located at src > Truss > xml. The main class is in the Truss directory. It reads the files fine when the program is run from eclipse however throws this error when exported to a jar and run: (this error is copied from cmd, not eclipse console)

java.lang.NullPointerException
        at java.io.FilterInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at Truss.main.initiate(main.java:167)
        at Truss.main.<init>(main.java:729)
        at Truss.main.main(main.java:110)

Here is the code:

try {
    InputStream in = main.class.getResourceAsStream("xml");
    BufferedReader input = new BufferedReader(new InputStreamReader(in));

    String profileName;

    while((profileName = input.readLine()) != null) {
        loadProfile(profileName.substring(0, profileName.length() - 4));
    }

} catch (Exception e) {
    e.printStackTrace();
}

The constructor for the while loop is line 167.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Alex A
  • 23
  • 8

2 Answers2

1

Obviously getResourceAsStream() returned null, and you didn't check it.

The resource requested was not in the JAR file under that name.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • How would I go about adding it to the jar file under that name? – Alex A Jul 20 '15 at 11:00
  • He's getting error on the while line. If "in" was null, he'd get NPE on the construction of the BufferedReader, not on the while line. Or did I get it wrong, @AlexA ? – Diego Martinoia Jul 20 '15 at 11:01
  • @AlexA With the JAR tool or your IDE. As you're looking for it as a file called `"xml"` in the same package as `main.class`, that's where it should be. – user207421 Jul 20 '15 at 11:01
  • @DiegoMartinoia The `BufferedReader` itself can't be null, or the `InputStreamReader`, so the input stream is the only other choice. – user207421 Jul 20 '15 at 11:03
  • Maybe I misunderstood OP's "The constructor for the while loop is line 167." I thought he meant he was throwing NPE on the while line. – Diego Martinoia Jul 20 '15 at 11:04
  • @DiegoMartinoia The OP's statement about the 'constructor for the while loop' is meaningless, but it *is* throwing on the `while` line. See the stack trace. It starts at the `readLine()` call. It's your assumption about the NPE when constructing that's at fault. Also there is no way that `while` loop can fail after a few iterations. – user207421 Jul 20 '15 at 11:05
  • I tried scratching that same example in a Java main: If I pass a null to either the BufferedReader or the InputStreamReader constructor as argument, I get thrown NPE immediately at the construction, not after I try to read from it. Maybe it's my IDE doing some magic. – Diego Martinoia Jul 20 '15 at 11:08
  • @DiegoMartinoia it is throwing an NPE on the while line from the readLine() method. – Alex A Jul 20 '15 at 11:08
  • @EJP I have the xml folder in the same directory as main.class however it still does not work. The 'xml' folder contains 2 xml files and I just need to retrieve their name, if that helps. – Alex A Jul 20 '15 at 11:09
  • @DiegoMartinoa Neither the `BufferedReader` nor the `InputStreamReader` can be null. They are both constructed with the `new` operator without any exception being thrown. In any case the code couldn't possibly get so far into their `readLine()` and `read()` methods respectively if either was null. The input stream is the only thing left. – user207421 Jul 20 '15 at 12:12
0

From the information you've shown, I can only guess you didn't package the resource files properly in the jar. Please have a look at this related question. Note that the resource folder should be under the src folder (where the .java files in), not the build folder (.class files).

Update

Just noticed that your "xml" is actually a folder, you can't use getResourceAsStream(). This answer shows a way of enumerating jar entry that may be your solution.

Community
  • 1
  • 1
t-benze
  • 386
  • 2
  • 8
  • The accepted answer to the linked question is incorrect. Resources are not files or directories, and cannot be listed with the `File` class. The poster's protestations notwithstanding. – user207421 Jul 20 '15 at 13:03
  • @EJP Yes, you're right...Seems like listing jar entry is the only solution? – t-benze Jul 20 '15 at 13:14