Fix null pointer exception bugs