Why do I get AttributeError: ‘NoneType' object has no attribute ‘something'?

Why do I get AttributeError: ‘NoneType' object has no attribute ‘something'?

In Python, we will get this type of error during coding. The error AttributeError: ‘NoneType’ object has no attribute ‘something’ occurs in Python when we try to access an attribute or method (something) on an object that is None.

In this blog, we will learn multiple cases where we get this type of error during coding in Python.

Table of Contents:

Python is a programming language that is used by developers to build scalable applications. While working with Python, we might find such type of error.

What Causes ‘AttributeError: NoneType Object Has No Attribute’ and How to Fix It?

Let’s see the multiple cases where we can get this type of error in Python:

1. The variable is not initialized or explicitly set to None

We can get the above error in Python when we have declared a variable and initialized it to None.

Let’s see the example for more clarity:

Code:

my_var = None

print(my_var.some_method())

Output:

python error

Solution of the error:

We should check the value of the variable is None, only then we would be able to access the method.

Code:

if my_var is not None:

    print(my_var.some_method())

2. The function you’re calling returns None

When we try to call a Python function that returns none, then this type of error occurs.

Let’s see the example for more clarity:

Code:

def example_function():

    pass 

result = example_function()

print(result.some_attribute)

Output:

ERROR!

Traceback (most recent call last):

  File "<main.py>", line 5, in <module>

AttributeError: 'NoneType' object has no attribute 'some_attribute'

Solution of the error:

We can use some default values for the cases where nothing is returned.

3.  The object you’re working with doesn’t exist or is missing

When we try to access an object that doesn’t exist or is missing, can give us this type of error.

Let’s see the example for more clarity:

Code:

data = {'key': None}

value = data.get('key')

print(value.attribute)

Output:

ERROR!

Traceback (most recent call last):

  File "<main.py>", line 3, in <module>

AttributeError: 'NoneType' object has no attribute 'attribute'

Solution of the error

We should check before accessing attributes or methods, that the object is not None.

Code:

if value is not None:

    print(value.attribute) 

else:

    print("The value is None. No attribute to access.")

4. Method chaining returns None

Some methods made the changes to the array or list itself, and returned None, and if we try to chain a method on this, this will give the above Attribute Error.

Let’s see the example for more clarity:

Code:

my_list = [1, 2, 3]

result = my_list.sort() 

print(result.append(4))

Output:

ERROR!

Traceback (most recent call last):

  File "<main.py>", line 3, in <module>

AttributeError: 'NoneType' object has no attribute 'append'

Solution of the error:

We should avoid chaining the methods that return None.

Conclusion

Python is an interpreted and open-source programming language that supports object-oriented programming. it is the most widely used programming language in the world.

So far in this article, we have learned four different methods to know why AttributeError is occurring in the Python Program. If you want to excel in your career in Python, you may refer to our Python Course.