Skip to content
Home » AttributeError: ‘NoneType’ object has no attribute ‘something’

AttributeError: ‘NoneType’ object has no attribute ‘something’

To solve AttributeError: ‘NoneType’ object has no attribute ‘something’ error follow below methods.

You’re encountering AttributeError: ‘NoneType’ object has no attribute’something’ because NoneType signifies you’ve got None instead of an instance of whichever Class or Object you thought you were dealing with. It signifies that the preceding assignment or function call failed or gave an unexpected result.

ERROR LOG

AttributeError: ‘NoneType’ object has no attribute ‘something’

AttributeError: The ‘NoneType’ object does not have the attribute’something’. One of the reasons is that NoneType implies that you have None instead of an instance of whatever Class or Object you are dealing with. It signifies that the function or assignment call failed or gave an unexpected result.


var = None
print(var.something)

Output:

AttributeError: ‘NoneType’ object has no attribute ‘something’

How to solve Why do I get AttributeError: ‘NoneType’ object has no attribute ‘something’ ?

Refer the given methods to solve the issue.

Method 1: Using if and else statements

By utilizing the- if and else statements, you can get AttributeError: ‘NoneType’ object has no attribute ‘something’. The goal here is to see if the object has a None value allocated to it. If it is None, simply print a comment noting that the value is Nonetype, which may interfere with the program’s execution.

a = None
if a is not None:
    a.some_attribute = "python"
else:
    print("Type of a :", type(a))

Output:

Type of a : <class ‘NoneType’>

Method 2 : Using try and except Blocks

Instead of if-else you can also use try and catch blocks to solve the error.

Hope the above solution works.

Also read :

ImportError: attempted relative import with no known parent package
TypeError: ‘int’ object is not callable in Python