NoneType does not support indexing

I have linked column in a table with a row from another table

I am getting this error.

error:

TypeError: ‘NoneType’ does not support indexing
** while setting self.label_1.text = self.item[‘user’][‘email’]**
** in a data binding for self.label_1**
at update_binding.py, line 2 column 2
TypeError: ‘NoneType’ does not support indexing
** while setting self.label_1.text = self.item[‘user’][‘email’]**
** in a data binding for self.label_1**
at update_binding.py, line 2 column 2

1 Like

@nciphavl I moved this to a new topic since it did not seem to relate to the original topic (AFAICT).

Can you post a clone link to your app or provide more information (e.g., the line of code that produces the error)? This way we can help more explicitly.

Follow these instructions to share a clone link:

However, it seems that you are returning a None and then indexing into it.

I’m happy to help more if you can provide more information.

PS) it is also a good idea to format your python code to make it easier for others to read. You can achieve this by enclosing your code inside of backticks with the word “python” like so:

```python
print(‘nicely formatted code’)

```

1 Like

I have found the solution

Hi there!

This error message tells you exactly what has gone wrong, even if there is some Python jargon in there. Let’s pull it apart:

TypeError: ‘NoneType’ does not support indexing

In Python “indexing” is a word for using square brackets on something. This error message means we tried to use square brackets on something that’s None (the empty value in Python), and that’s not allowed.

while setting self.label_1.text = self.item['user']['email']

Aha! Look at what we’re using square brackets on. If this line exploded, then either self.item or self.item['user'] is None.

Given that you said you were linking tables together, my guess is that self.item is a row from a table with a column called 'user' which links to a row from the Users table. Am I right? If I am, then this error is probably happening when we try to display a row that isn’t linked to any row from the Users table. (Its user column is None – so self.item['user'] is None, and trying to use square brackets on it will produce an error.)

So, there are two possibilites here:

  1. Our code is correct, but we are feeding it bad data. (In this case, the table should never have a row with an empty 'user' column, and it’s understandable that it throws errors if you give it malformed data.)

    If this is the case, we need to edit the database to make sure every row is linked to a user – and fix any code that’s creating rows without users!

  2. Our data is correct, and our code should be able to deal with it.

    If this is the case, we need to change that expression to be able to cope with self.item['user'] being None.

Where is this error happening?

in a data binding for self.label_1

Well, that’s where we’ll have to fix it! Open the form that has a data binding on label_1 (it’s probably a template form inside a RepeatingPanel or a DataGrid), and then select label_1.

The data binding is currently:

self.item['user']['email']

As we’ve seen, this doesn’t work when the 'user' column is set to None. But the following code works - it tests whether the 'user' column is None before indexing into it, and returns a different message if that column is empty:

self.item['user']['email'] if self.item['user'] is not None else 'No User linked to this row'

I hope this step-by-step guide has helped show how you can walk through error messages like these and make sense of them!

6 Likes

Sorry guys I made a mistake the user column had no values.

2 posts were split to a new topic: Comparing string to each value in row - NoneType not iterable

Hi, greetings Meredydd. I am reviving this old thread because I am having this same problem as the OP, and I don’t see how the above is solving it. Because, as you can see, there are absolutely no blank records in any table in my DB. All rows in all tables are full. So there is no question of NoneType, correct? I have attached the clone here for you to see. It’s just basic to showcase my point, but I am facing this same issue in my ‘real’ apps too. Please help, thanks.

https://anvil.works/build#clone:LDGDS3UTUR5PUBD2=EYOSD7OGT4B6UPJVWGE7Y7JS

@rjsharma I had a quick look

The pay column is all None types in the clone you posted

changing the label_2 binding to:

self.item['pay']['salary'] if self.item['pay'] is not None else "No pay supplied"

works as described above

A post was split to a new topic: Linked rows question