Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am getting an error message that says
AttributeError: 'NoneType' object has no attribute 'something'
How can I understand this message?
What general scenarios might cause such an AttributeError
, and how can I identify the problem?
This is a special case of AttributeError
s. It merits separate treatment because there are a lot of ways to get an unexpected None
value from the code, so it's typically a different problem; for other AttributeError
s, the problem might just as easily be the attribute name.
See also What is a None value? and What is a 'NoneType' object? for an understanding of None
and its type, NoneType
.
–
–
You have a variable that is equal to None and you're attempting to access an attribute of it called 'something'.
foo = None
foo.something = 1
foo = None
print(foo.something)
Both will yield an AttributeError: 'NoneType'
–
–
The NoneType
is the type of the value None
. In this case, the variable lifetime
has a value of None
.
A common way to have this happen is to call a function missing a return
.
There are an infinite number of other ways to set a variable to None, however.
–
–
It means the object you are trying to access None
. None
is a Null
variable in python.
This type of error is occure de to your code is something like this.
x1 = None
print(x1.something)
x1 = None
x1.someother = "Hellow world"
x1 = None
x1.some_func()
# you can avoid some of these error by adding this kind of check
if(x1 is not None):
... Do something here
else:
print("X1 variable is Null or None")
When building a estimator (sklearn), if you forget to return self in the fit function, you get the same error.
class ImputeLags(BaseEstimator, TransformerMixin):
def __init__(self, columns):
self.columns = columns
def fit(self, x, y=None):
""" do something """
def transfrom(self, x):
return x
AttributeError: 'NoneType' object has no attribute 'transform'?
Adding return self
to the fit function fixes the error.
–
–
You can get this error with you have commented out HTML in a Flask application. Here the value for qual.date_expiry is None:
<!-- <td>{{ qual.date_expiry.date() }}</td> -->
Delete the line or fix it up:
<td>{% if qual.date_attained != None %} {{ qual.date_attained.date() }} {% endif %} </td>
None of the other answers here gave me the correct solution. I had this scenario:
def my_method():
if condition == 'whatever':
return 'something'
else:
return None
answer = my_method()
if answer == None:
print('Empty')
else:
print('Not empty')
Which errored with:
File "/usr/local/lib/python3.9/site-packages/gitlab/base.py", line 105, in __eq__
if self.get_id() and other.get_id():
AttributeError: 'NoneType' object has no attribute 'get_id'
In this case you can't test equality to None
with ==
. To fix it I changed it to use is
instead:
if answer is None:
print('Empty')
else:
print('Not empty')
–