Fix bugs with NullPointerExeption