Fix NullPointerException bugs