How can i solve TypeError: 'NoneType' object is not subscriptable error

Am currently making a program to find a place’s id
my code looks like this

import json
import urllib.request, urllib.parse, urllib.error

serviceurl = 'http://py4e-data.dr-chuck.net/json?'


address = "De Anza College"

url = serviceurl + urllib.parse.urlencode(
        {'address': address})

print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read().decode()
print('Retrieved', len(data), 'characters')

try:
        js = json.loads(data)
except:
        js = None

    
place_id = js['results'][0]['place_id']
print('Place id',place_id)

but the same error keeps popping up

Traceback (most recent call last):
File “main.py”, line 24, in place_id = js[‘results’][0][‘place_id’]TypeError: ‘NoneType’ object is not subscriptable

what can i do to fix this
thanks in advance

You are not getting from the page answer you expect. Try opening that page in your browser with the address=“De Anza College” parameter.

Welcome, metaxas.

The error is saying that your variable js is of type None, which is what you declared it to be in your try...except block. Therefore, there is some error with the json.loads(data) call you have.

There is little point to a try..except block if you are not getting the errors from it. Add a catch and print the error. It will help a lot.

I am experiencing the same error . Can you help me out

The error is self-explanatory. You are trying to subscript an object which you think is a list or dict, but actually is None. This means that you tried to do:

None[something]

‘NoneType’ object is not subscriptable error means that you attempted to index an object that doesn’t have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python.

Visit here: typeerror nonetype object is not subscriptable ,
there is a solution in above link for errors .

Python - TypeError – NoneType Object not Subscriptable

import numpy as np   # Import numpy package for mathematical operations

defaddition1(arrays):                   # Function to perform addition
total1=arrays.sum(axis=1)          # Row-wise sum in 2D numpy array
print('row wise sum',total1[0:4])  # Printing first 4 elements of total

a=np.arange(12).reshape(6,2)  # Creating a 2D array
print('input array \n',a)               # Printing a comment
print('*********')
addition1(a)                              # Calling the function
input array
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]]

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.