fix NPE and format code