Struggling with NullPointer and ClassCastException