title: Why does InputStreamReader throw a NPE when reading from a jar?
body: <p>I'm trying to iterate over the class files in a known directory using streams. The ultimate goal is to obtain the class names for all classes that exist in a particular package, then load the classes at runtime and use reflection to obtain the names &amp; values of all the static constants. This works when I run the program from source on my machine, but when I run it as a jar the <code>BufferedReader</code> throws a NPE from both <code>ready()</code> and <code>readLine()</code>. Here's the code (with error handling &amp; best practices omitted for brevity):</p>
  
  <pre><code>private void printClassNamesInPackage(final String strPackage) throws Exception {
      // The returned implementation of InputStream seems to be at fault
      final InputStream       packageStream = getClass().getClassLoader().getResourceAsStream( strPackage );
      final InputStreamReader streamReader  = new InputStreamReader( packageStream );
      final BufferedReader    reader        = new BufferedReader( streamReader );
      // Throws NPE from inside ready() - SEE STACKTRACE BELOW
      // reader.ready()
      String strLine;
      // Throws NPE from inside readLine() -  SEE STACKTRACE BELOW
      while ( null != (strLine = reader.readLine()) ) {
          System.out.println( strLine );
      }
  }
  </code></pre>
  
  <p><em>The stacktrace from <code>reader.ready()</code></em>:</p>
  
  <pre><code>java.lang.NullPointerException
      at java.io.FilterInputStream.available(FilterInputStream.java:142)
      at sun.nio.cs.StreamDecoder.inReady(StreamDecoder.java:343)
      at sun.nio.cs.StreamDecoder.implReady(StreamDecoder.java:351)
      at sun.nio.cs.StreamDecoder.ready(StreamDecoder.java:165)
      at java.io.InputStreamReader.ready(InputStreamReader.java:178)
      at java.io.BufferedReader.ready(BufferedReader.java:436)
  </code></pre>
  
  <p><em>The stacktrace from <code>reader.readLine()</code></em>: </p>
  
  <pre><code>java.lang.NullPointerException
      at java.io.FilterInputStream.read(FilterInputStream.java:116)
      at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
      at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
      at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
      at java.io.InputStreamReader.read(InputStreamReader.java:167)
      at java.io.BufferedReader.fill(BufferedReader.java:136)
      at java.io.BufferedReader.readLine(BufferedReader.java:299)
      at java.io.BufferedReader.readLine(BufferedReader.java:362)
  </code></pre>
  
  <p>Stepping through the execution reveals the following <code>InputStream</code> implementations:</p>
  
  <ul>
  <li>From source: <code>java.io.ByteArrayInputStream</code></li>
  <li>From jar: <code>sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream</code></li>
  </ul>
  
  <p>Looking further into <code>JarURLInputStream</code> I find that the inherited (from <code>FilterInputStream</code>) field <code>InputStream in</code> is <code>null</code>, which leads to the resulting NPE.  </p>
  
  <p>Unfortunately, that's as deep as I was able to get in the debugger. </p>
  
  <p>Any ideas on how to properly do this? What am I missing or doing wrong? Thank you!</p>