fixing some null pointer and class cast exceptions