Fix NPE and code style