Code Fix

中級AttributeError: 'NoneType' object has no attribute の原因と直し方 › パターン3

AttributeError: 'NoneType' object has no attribute の原因と直し方

Noneに対してメソッドを呼び出したときに出るエラーです。

 1  user = {"name": "Tom"}
 2  email = user.get(_______)
                        ^
 3  print(email.strip())
Traceback (most recent call last): File "main.py", line 3, in <module> print(email.strip()) ^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'strip'

空欄を埋めて実行を通す

広告
広告スロット(未設定)

AttributeError: 'NoneType' object has no attribute の原因と直し方

関数が値を見つけられずNoneを返す場面や、辞書のgetでキーが無かった場合に起きやすいパターンです。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)