Fix calling length on nil object

Instead of just checking the length, check if the object is nil first, and then check and see if the string is emtpy