check for null before closing stream