fix bug by trapping null pointer error