我在java中提取ZIP文件:
ZipFile zipFile = new ZipFile(theZipFile);
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
while(zipEntries.hasMoreElements()){
ZipEntry entry = zipEntries.nextElement(); /// <---Nullpointer exception happens here
}
代码执行过去了(zipEntries.hasMoreElements())但在提取ZipEntry时失败了.
很奇怪hasMoreElements返回true,但是在尝试获取元素时会出现空指针.
异常来自JDK lib的ZipFile类,我在调试器中看不到局部变量,所以如何找出Zip文件的错误?
编辑:
堆栈跟踪:
java.lang.NullPointerException
at java.util.zip.ZipFile.getZipEntry(ZipFile.java:529)
at java.util.zip.ZipFile.access$900(ZipFile.java:56)
at java.util.zip.ZipFile$1.nextElement(ZipFile.java:511)
at java.util.zip.ZipFile$1.nextElement(ZipFile.java:481)
解决方法:
这是getZipEntry方法(截至1.7.0_10):
private ZipEntry getZipEntry(String name, long jzentry) {
ZipEntry e = new ZipEntry();
e.flag = getEntryFlag(jzentry); // get the flag first
if (name != null) {
e.name = name;
} else {
byte[] bname = getEntryBytes(jzentry, JZENTRY_NAME);
if (!zc.isUTF8() && (e.flag & EFS) != 0) {
e.name = zc.toStringUTF8(bname, bname.length);
} else {
e.name = zc.toString(bname, bname.length); // Line 529
}
}
/* snip */
return e;
}
如果e,zc或bname为null,则在此行上抛出NullPointerException的唯一原因.
e不能为null,因为它在此方法中明确实例化.
zc不能为null:
public ZipFile(File file, int mode, Charset charset) throws IOException
{
/* snip */
this.zc = ZipCoder.get(charset);
/* snip */
}
static ZipCoder get(Charset charset) {
return new ZipCoder(charset);
}
这意味着bname必须为null,这将很难调试. getEntryBytes是一个本机方法:
private static native byte[] getEntryBytes(long jzentry, int type);
这是我将如何进行:
>弄清楚它是特定的zip文件还是所有zip文件.如果是特定的zip文件,请尝试重新制作.
>更新您的Java版本,可能是修复了getEntryBytes的错误
>向Oracle提交错误报告