java.lang.NullPointerException:Inflater 已在 InputStreamReader 上关闭
我遇到以下问题:
当我从项目加载资源时一切正常,但是当我将其放在 .jar 上并尝试加载时出现异常。
代码是:
InputStreamReader reader =
new InputStreamReader(
this.getClass().getResource(fileName).openStream())
CsvReader stream = new CsvReader(reader);
try {
while (stream.readRecord()) {
line = stream.getRawRecord();
//DO SOMETHING...
}
}catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
stream.close();
}
stackTrace 是:
Inflater has been closed java.lang.NullPointerException: Inflater has been closed
at java.util.zip.Inflater.ensureOpen(Inflater.java:364)
at java.util.zip.Inflater.inflate(Inflater.java:237)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:135)
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 utils.csvreader.CsvReader.checkDataLength(CsvReader.java:1198)
at utils.csvreader.CsvReader.readRecord(CsvReader.java:604)
问题发生在 stream.getRawRecord()
上。我还尝试使用 BufferedReader 在 readLine(); 上得到相同的结果
有什么想法吗?
I have the following problem:
When I load the resource from the project all works fine, but when I put it on a .jar and try to load I get an exception.
The code is:
InputStreamReader reader =
new InputStreamReader(
this.getClass().getResource(fileName).openStream())
CsvReader stream = new CsvReader(reader);
try {
while (stream.readRecord()) {
line = stream.getRawRecord();
//DO SOMETHING...
}
}catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
stream.close();
}
And the stackTrace is:
Inflater has been closed java.lang.NullPointerException: Inflater has been closed
at java.util.zip.Inflater.ensureOpen(Inflater.java:364)
at java.util.zip.Inflater.inflate(Inflater.java:237)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:135)
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 utils.csvreader.CsvReader.checkDataLength(CsvReader.java:1198)
at utils.csvreader.CsvReader.readRecord(CsvReader.java:604)
The problem happens on stream.getRawRecord()
. I also tried with BufferedReader with same result on readLine();
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是 Java 中的错误。我遇到了类似的问题,如下所述: https://issues.apache.org/jira/ browser/FELIX-1032
您的应用程序是否使用某些特殊的类加载器设置运行?
This can be bug in the Java. I had a similar problem, described here: https://issues.apache.org/jira/browse/FELIX-1032
Is your application running with some special class loader settings?
从项目加载资源意味着您不处理zip(jar是zip),因此InputStreamReader解析为您想要读取的文件。
我的猜测是,InputStreamReader 在处理 jar 时解析为 null 或错误的文件。记录变量以确保
loading resource from project means that you do not deal with zip (jar is zip), so InputStreamReader resolves to file you want to read.
My guess is that InputStreamReader resolves to null or wrong file when dealing with jar. Log vars to be asure