AttributePython Error

AttributeError

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

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    print(result.count(1))  # result is None
AttributeError: 'NoneType' object has no attribute 'split'

What causes this error

An attribute or method was accessed on an object that does not have it. This frequently happens when a variable is None unexpectedly, when there is a typo in the attribute name, or when working with the wrong type.

How to fix it

Check what type the object actually is with `type(obj)`. Look for functions that return None when you expected a value. Use `hasattr()` or `getattr()` with a default to safely access attributes. Enable type checking to catch these at development time.

Code that causes this error

Broken
result = [3, 1, 2].sort()
print(result.count(1))  # result is None

Fixed code

Fixed
data = [3, 1, 2]
data.sort()
print(data.count(1))

About AttributeError

An AttributeError is raised when you try to access an attribute or method that does not exist on an object. This is one of the most frequent Python runtime errors, and it often indicates a misunderstanding about an object's type or a None value sneaking in where a real object was expected. The classic scenario is calling a method on the return value of a function that returns None — for example, list methods like `.append()`, `.sort()`, and `.reverse()` modify the list in place and return None, so chaining calls on their result produces an AttributeError.

Other causes include typos in method names, using an attribute from the wrong class, and accessing attributes on objects whose type changes dynamically. Python 3.10+ provides suggestions for similar attribute names in the error message, which helps quickly identify typos.

Common scenarios

1

Calling methods that do not exist on the object's type (e.g., .push() on a list)

2

Working with None values from functions that did not return explicitly

3

Using methods from one type on a different type by mistake

4

Accessing attributes on objects whose type changed due to reassignment

Related errors