check if item.to_object returns an object before trying to retrieve item.code etc