Fix null pointer exception in writing a file.