How to debug a NullPointerException in library code?

I am extracting a zip file in java:

ZipFile zipFile = new ZipFile(theZipFile); Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); while(zipEntries.hasMoreElements()){ ZipEntry entry = zipEntries.nextElement(); /// <---Nullpointer exception happens here } 

Code execution after while(zipEntries.hasMoreElements()) but failed to retrieve ZipEntry.

It is strange that hasMoreElements returns true, but has a null pointer when trying to retrieve elements.

An exception to the ZipFile class from the JDK lib that I do not see local variables in the debugger, so how do I know what is wrong with the Zip file?

Edit: Stack Trace:

 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) 
+4
source share
2 answers

Here is the getZipEntry method (starting from 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; } 

The only reason a NullPointerException will be thrown on this line would be if e , zc or bname were null .

e cannot be null because it is explicitly created in this method.

zc cannot be 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); } 

This means that bname must be null , which is pretty hard to debug. getEntryBytes is a native method:

 private static native byte[] getEntryBytes(long jzentry, int type); 

Here's how I will continue:

  • Find out if this is a particular zip file or all zip files. If this is a specific zip file, try redoing it.
  • Update your version of Java, there may be an error with getEntryBytes that has been fixed.
  • Submit Error Report to Oracle
+3
source

I recently had the same problem, so I compared JDK 1.6 and 1.7 postcode .

The real reason jdk 1.6 will be the initial zipEntry with the jzentry constructor . > like this:

 ZipEntry ze = new ZipEntry(jzentry); ZipEntry(long jzentry) { initFields(jzentry); } 

but in jdk 1.7 we see that it uses

 private ZipEntry getZipEntry(String name, long jzentry) { ZipEntry e = new ZipEntry(); // it did not use jzentry initial it 

If there is an empty entry in the jar file, this will be a throw exception.

0
source

All Articles