Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Having null issues <solved>


I just started coding nine days ago, and I thought I was getting used to it, but I’m still having fundamental issues. Below is a copy of the code that I wrote, it’s giving me a null error on the distance = hero.distanceTo(item) line. Can anyone explain this to me because I am having a very hard time figuring this out?

The artillery uses coins as a target.

You’ll be the rangefinder for the artillery.

Write the function.

def coinDistance():
# Find the nearest coin,
item = hero.findNearestItem()
# If there is a coin, return the distance to it.
distance = hero.distanceTo(item)
if item:
return distance
# Else, return 0 (zero).
return 0


If there are no items in game yet this function returns null instead of an array of items. You can check for nulls by using an if-then-else statement before calling the distance = code segment.


Ok, I got it. If I am calling the distance to an item that hasn’t appeared yet, it will be equal to null. Thank you for your help.


@Nadario_Seays you are welcome.

Also, when you edit the title, there should actually be a category called Solved that you can choose. This helps categorize solutions into a single area for others who might have similar issues.



I looked but didn’t find a solved category.


@Nadario_Seays you are right, how odd. there is a “solved” category that is available in the forums if you select the category listing. I wonder if this is only something that can be chosen by the forum moderators?

Sorry about that, I changed your title back to adding the <solved> to is as this helps the rest of us skip the post if we know its resolved.

Thank you for trying to mark it as solved.


No worries my friend, no worries!!