Another nullpointerexception

directory is allowed to be null, so it must be taken care of.
Avoid lookup of ApplicationContext within dispose