2012-12-11 62 views
1

我得到一個:的Python處理一個類型錯誤:

TypeError: 'NoneType' object has no attribute '__getitem__' 

錯誤我的程序查詢我的分貝。問題是,它然後停止我不想爲Web應用程序發生的整個程序。

相反,我該如何處理這個優雅,因爲它必須指向一個空的領域,我不會關心它。這是一個代碼片段:

try: 
    if not doc['coordinates']: 
     xxxxxxxxx 
    else: 
     xxxxxxxx 

except (ValueError, geocoders.google.GQueryError): 
    pass 

我試過,包括在except()一個TypeError,而導致在客戶端上的問題。

感謝

+1

什麼是'doc'應該是什麼?現在是'沒有'。 –

+1

包含TypeError時,客戶端會遇到什麼問題? – Himanshu

+0

這是數據庫查詢的結果。 – user1869421

回答

2

如何: -

if doc: 
    try: 
     if not doc['coordinates']: 
      xxxxxxxxx 
     else: 
      xxxxxxxx 

    except (ValueError, geocoders.google.GQueryError): 
     pass 

或者這樣: -

try: 
     if not doc or not doc['coordinates']: 
      xxxxxxxxx 
     else: 
      xxxxxxxx 

    except (ValueError, geocoders.google.GQueryError): 
     pass 
+0

我以爲這會工作,但它給我一個錯誤,我的谷歌geopy api。 – user1869421

+0

謝謝,但是我仍然遇到與上述相同的問題。 。 – user1869421

+0

在這種情況下,我不能幫助,因爲這需要更好地理解您的具體問題 – Himanshu