Add check for null values in length function