I get a NullPointerException everytime I try to obtain the content of my BufferedReader, when I run the project that is already packed in jar. When it runs inside of IDE (IntelliJ) there's no any problem. I have already checked all previous InputStreams, they seem to be all right. Have you maybe some tipp? Thank you in advance!
String pathToFontDir = "/fonts/Open_Sans";
GraphicsEnvironment localGE = GraphicsEnvironment.getLocalGraphicsEnvironment();
final InputStream is = Session.class.getResourceAsStream(pathToFontDir);
if(is != null){
final InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
final BufferedReader br = new BufferedReader(isr);
List fonts = br.lines().collect(Collectors.toList()); //throws NullPointerException
if(fonts.size() > 0){
for(Object font: fonts){
InputStream fis = loader.getResourceAsStream(pathToFontDir + "/" + font);
localGE.registerFont(Font.createFont(Font.TRUETYPE_FONT, fis));
}
} else{
throw new IOException("Der angegebene Ordner enthält keine Schriftdateien!");
}
}
I also tried just to check, wheather my BufferedReader contains anything, but it throwed an exception again without returning any line:
String fontName;
while((fontName = br.readLine()) != null) //throws NPE as well
System.out.println(fontName);
The stack trace:
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 java.io.BufferedReader$1.hasNext(Unknown Source) at java.util.Iterator.forEachRemaining(Unknown Source) at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source) at java.util.stream.AbstractPipeline.copyInto(Unknown Source) at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source) at java.util.stream.AbstractPipeline.evaluate(Unknown Source) at java.util.stream.ReferencePipeline.collect(Unknown Source) at main.java.backend.Session.importFontResource(Session.java:137) at main.java.backend.Session.main(Session.java:180)
You cannot read a resource directory as such. As there is no easy provision in standard java, make a "directory" listing in a resource file, and read that. Keep it conventional.
For the "uneasy" solution: http://www.uofr.net/~greg/java/get-resource-listing.html