Properly cast objects to handle null pointers and non-string objects