TypeError: 'NoneType' object is not subscriptable

(This article explains how to debug this error message in the context of "Week 5 Implement Conversations with DynamoDB" at the AWS Cloud Project Bootcamp by Andrew Brown and Exampro Team.)

Debugging is a particularly challenging job for newbie programmers because the error messages don't necessarily clearly tell you what's wrong.

The topic error message means that the data object you are dealing with cannot be "subscripted". So what does it mean?

Meaning of Subscriptable

[2023-05-07 16:27:15,623] ERROR in app: Exception on /api/messages/5ae290ed-55d1-47a0-bc6d-fe2bc2700399 [GET]**
Traceback (most recent call last):
 ...
  File "/backend-flask/app.py", line 247, in data_messages
    **if model['errors'] is not None:**
**TypeError: 'NoneType' object is not subscriptable**
192.168.30.138 - - [07/May/2023 16:27:15] "GET /api/messages/5ae290ed-55d1-47a0-bc6d-fe2bc2700399 HTTP/1.1" 500 -

Subscripting is a form of notation that gives order or a label to each item in a set. They do that a lot in chemistry (chemical formulas). Superscript, a lot in math (exponentiation). In the context of Python and other programming languages, "subscriptable" means the data "can be indexed".

Indexable (subscriptable) datatypes

  • string: "this string can be can be indexed"

  • list: [ 'this', 'list', 'can', 'be', 'indexed', 'too' ]

  • tuple: (505, 500, 401, 200)

  • dictionary

Unindexable datatypes

  • int

  • bool

  • NoneType

Judging from the model['errors'] in the error message, the model is expected to have the following structure. As the error message points out, the data model is None, so it means there's nothing there:

# Expectation 
model = {
    ...
    'errors': 'something' 
    ...
}

# Actually
model = None  # <----------- Cannot be indexed / subscripted.

Where to look👀

Obviously, something's wrong with model. So you want to debug, yes? Where to look👀? Go look at the lines that have that model.

Application