Fixes findbugs warning - dereferencing null in exception case

Prevent NullPointerExceptions when trying to close a resource in case of an exception.