title: How to troubleshoot and resolve "TypeError: 'NoneType' object is not subscriptable" in python
body: <p>I have a piece of code that fetches data from the ticketmaster API using a function I've named <code>get_event_info</code>. The first revision of the code worked as desired, subsequently I modified the original function to make use of header based authentication instead of URL based. I also added a few lines to the function which were intended to validate the response status code. After making these changes the code began producing the following <code>TypeError</code>:</p>
  
  <pre><code>Traceback (most recent call last):
    File "ticketmaster_only_w_headers.py", line 146, in &lt;module&gt;
      for event in ticket_search["_embedded"]["events"].items():
  TypeError: 'NoneType' object is not subscriptable
  </code></pre>
  
  <p>I've read quite a bit about this type of error but I'm still unable to determine why my code is producing it in this instance. I would really appreciate an explanation on why my code is producing this error and what troubleshooting methods I should have used to uncover the source error. I'm fairly comfortable with programming but certainly no expert so the simpler the language used the better.  </p>
  
  <p>(Function Definition)</p>
  
  <pre><code>def get_event_info(search):
      if search in CACHE_DICTION:
          d = CACHE_DICTION[search]
      else:
          api_url = '{0}events/'.format(api_url_base)        
          payload = {"keyword": search, "apikey": api_token,
                     "format": "json", "dmaId": "366", "size": 200, "radius": "2"}
          response = requests.get(api_url, headers=headers, params=payload)
          if response.status_code == 200:
              d = json.loads(response.text)
              CACHE_DICTION[search] = d
              f = open(CACHE_FNAME, 'w')
              f.write(json.dumps(CACHE_DICTION))
              f.close()
          else:
              d = None
      return d
  </code></pre>
  
  <p>(Code snippet that triggers the error)</p>
  
  <pre><code>ticket_search = get_event_info("")
  
  for event in ticket_search["_embedded"]["events"]:
      a = event["id"]
      b = event["name"]
      if "dateTime" in event["dates"]["start"]:
          c = event["dates"]["start"]["dateTime"].replace(
              "T", " ").replace("Z", "")
      else:
          c = "NONE"
      if "end" in event["dates"] and "dateTime" in event["dates"]["end"]:
          j = event["dates"]["end"]["dateTime"].replace(
              "T", " ").replace("Z", "")
      else:
          j = "NONE"
  </code></pre>
  
  <p>(Code that creates, opens, and writes to the cache used in the above code)</p>
  
  <pre><code>
  CACHE_FNAME = "ticketmaster_cache.json"                                         
  try:
      cache_file = open(CACHE_FNAME, "r")                                         
      cache_contents = cache_file.read()                                          
      CACHE_DICTION = json.loads(cache_contents)                                  
      cache_file.close()                                                          
  except:
      CACHE_DICTION = {}
  </code></pre>
  
  <hr>
  
  <p>The previous revision of the <code>get_event_info</code> function shown below which does not produce any <code>TypeError</code>:</p>
  
  <pre><code>def get_event_info(search, ticketmaster_key = ticketmaster_key):                                                                                
      if search in CACHE_DICTION:                                         
          d = CACHE_DICTION[search]
      else:                                                                       
          data = requests.get("https://app.ticketmaster.com/discovery/v2/events", 
              params = {"keyword": search, "apikey": ticketmaster_key,            
              "format":"json", "dmaId": "366", "size": 200, "radius": "2"})
          print(data.url)
          d = json.loads(data.text)                                       
          CACHE_DICTION[search] = d                                      
          f = open(CACHE_FNAME, 'w')                                      
          f.write(json.dumps(CACHE_DICTION))                             
          f.close()                                                       
      return d
  </code></pre>
  
  <p>Traceback &amp; Error message I see when I run the latest revision of the code:</p>
  
  <pre><code>Traceback (most recent call last):
    File "ticketmaster_only_w_headers.py", line 146, in &lt;module&gt;
      for event in ticket_search["_embedded"]["events"]:
  TypeError: 'NoneType' object is not subscriptable
  </code></pre>