how to ignore AttributeError: 'NoneType'

Donnie Z picture Donnie Z · Sep 24, 2015 · Viewed 9.9k times · Source
location = geolocator.geocode(" ".join(address.values()))
if location.longitude is not None:
    node['pos'] = [location.longitude, location.latitude]

Don't understand way I'm still getting this error:

  File "/home/easypc/Documents/Udacity_nano_degree/Data_Wrangling/audit_vilnius.py", line 167, in shape_element
    if location.longitude is not None:
AttributeError: 'NoneType' object has no attribute 'longitude'

Answer

Martijn Pieters picture Martijn Pieters · Sep 24, 2015

It is the location variable itself that is None, test for that too:

if location is not None and location.longitude is not None:

or perhaps more simply put:

if location: