Fix several null pointer exception possibility and how to deal with integer overflow