JDK-6924361 : NPE from DeleteOnExitHook.add
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2010-02-08
  • Updated: 2010-04-03
  • Resolved: 2010-02-09
Related Reports
Duplicate :  
I was running an Ant build on JDK 6 from bash and pressed Ctrl-C to stop it. It stopped, but also printed a stack trace:

	at java.io.DeleteOnExitHook.add(DeleteOnExitHook.java:34)
	at java.io.File.deleteOnExit(File.java:939)
...Ant code...

EVALUATION Possible duplicate of 6526376 (fixed in jdk7).

SUGGESTED FIX While I have no idea how to reproduce the exception, from looking at the code it is clear it is wrong: synchronized(files) { // <=== NPE here if(files == null) throw new IllegalStateException("Shutdown in progress"); files.add(file); } If files is null then attempts to synchronize on it will throw NPE, so (assuming other accesses to the field are similarly synchronized) the null check is dead code. (FindBugs probably catches this kind of mistake.) Possible solutions: 1. Move the null check outside the synchronized block. Would usually prevent this exception, but is subject to a race condition. 2. Use a different monitor than files, making sure it is final and non-null: private final Object LOCK = new String("DeleteOnExitHook.LOCK"); 3. Continue to synchronize on files but ensure it is always non-null (preferably also final), and use some other means of indicating that a shutdown is in progress, such as a boolean flag.