2013-12-16 64 views
1

在Android中,我嘗試使用Yelps API v2解析查詢的結果。我得到正確的JSON,但由於某種原因,我得到空指針異常的錯誤。以下是一些代碼片段。解析Yelp API v2 JSON空指針異常

class RetreiveSearchResults extends AsyncTask<String, Void, Void> { 
@Override 
protected Void doInBackground(String... terms) { 
expListView = (ExpandableListView) findViewById(R.id.lvExp); 


OAuthService service = new ServiceBuilder().provider(YelpApi2.class).apiKey("myKey").apiSecret("myKey").build(); 
Token accessToken = new Token("wKXleYnSD0RH_qhj-myKey-UG", "myKey"); 
OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search"); 
request.addQuerystringParameter("location", "Waterfront, Boston, MA"); 
request.addQuerystringParameter("category", category); 
service.signRequest(accessToken, request); 
Response response = request.send(); 
String rawData = response.getBody(); 

try { 
    JSONObject json = new JSONObject(rawData); 
    JSONArray businesses; 
    businesses = json.getJSONArray("businesses"); 
    for (int i = 0; i < businesses.length(); i++) { 
      JSONObject business = businesses.getJSONObject(i); 
      businessNames.add(business.getString("name")); 
    } 
for(int j = 0; j < businessNames.size(); j++){ 
    listDataChild.put(businessNames.get(j), businessInfo); 
} 
} catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

ERROR:

線125是上述businessNames.add(business.getString("name"));

12-16 14:45:46.854: E/AndroidRuntime(28707): FATAL EXCEPTION: AsyncTask #2 
12-16 14:45:46.854: E/AndroidRuntime(28707): java.lang.RuntimeException: An error occured while executing doInBackground() 
12-16 14:45:46.854: E/AndroidRuntime(28707): at android.os.AsyncTask$3.done(AsyncTask.java:299) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at java.util.concurrent.FutureTask.setException(FutureTask.java:124) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at java.lang.Thread.run(Thread.java:856) 
12-16 14:45:46.854: E/AndroidRuntime(28707): Caused by: java.lang.NullPointerException 
12-16 14:45:46.854: E/AndroidRuntime(28707): at com.td.rssreader.CoffeeResultActivity$RetreiveSearchResults.doInBackground(CoffeeResultActivity.java:125) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at com.td.rssreader.CoffeeResultActivity$RetreiveSearchResults.doInBackground(CoffeeResultActivity.java:1) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at android.os.AsyncTask$2.call(AsyncTask.java:287) 
12-16 14:45:46.854: E/AndroidRuntime(28707): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
12-16 14:45:46.854: E/AndroidRuntime(28707): ... 5 more 

我登錄第一business的JSONObject的輸出,這是我收到的JSON輸出...

{ 
    "rating_img_url_large":"http:\/\/s3-media4.ak.yelpcdn.com\/assets\/2\/www\/img\/9f83790ff7f6\/ico\/stars\/v1\/stars_large_4_half.png", 
    "snippet_text":"Last night got off to a somewhat inauspicious start. As usual, I was dependent on my GPS to get us to our destination for the evening. Also as usual, I also...", 
    "phone":"6176549900", 
    "menu_date_updated":1383451144, 
    "rating_img_url":"http:\/\/s3-media2.ak.yelpcdn.com\/assets\/2\/www\/img\/99493c12711e\/ico\/stars\/v1\/stars_4_half.png", 
    "location":{ 
     "neighborhoods":[ 
     "Waterfront", 
     "Leather District", 
     "South Boston" 
    ], 
    "state_code":"MA", 
    "display_address":[ 
    "9 East St", 
    "Waterfront", 
    "Boston, MA 02111" 
    ], 
    "address":[ 
    "9 East St" 
    ], 
    "country_code":"US", 
    "postal_code":"02111", 
    "city":"Boston" 
    }, 
    "menu_provider":"single_platform", 
    "review_count":340, 
    "is_closed":false, 
    "is_claimed":true, 
    "rating_img_url_small":"http:\/\/s3-media2.ak.yelpcdn.com\/assets\/2\/www\/img\/a5221e66bc70\/ico\/stars\/v1\/stars_small_4_half.png", 
    "url":"http:\/\/www.yelp.com\/biz\/o-ya-boston", 
    "id":"o-ya-boston", 
    "image_url":"http:\/\/s3-media3.ak.yelpcdn.com\/bphoto\/pyRoQtCY4ou8_VvSikmidw\/ms.jpg", 
    "name":"O Ya", 
    "display_phone":"+1-617-654-9900", 
    "snippet_image_url":"http:\/\/s3-media4.ak.yelpcdn.com\/photo\/QLLDC2CmRvTvFjullAC7tg\/ms.jpg", 
    "mobile_url":"http:\/\/m.yelp.com\/biz\/o-ya-boston", 
    "categories":[ 
     [ 
     "Japanese", 
     "japanese" 
     ] 
    ], 
    "rating":4.5 
} 

正如你所看到的。 「名字」在那裏,但我無法抓住它。

回答

1

我沒有看到businessNames對象聲明或初始化在代碼中的任何地方。我敢打賭你宣佈它在某個地方,但沒有初始化它。 此外,「評級」的價值似乎並不是String而是Integer,所以不要使用.getString(),innit?

+0

商家名稱是在類頂部聲明的字符串列表。我的意思是有「名」而不是「評級」。我編輯過。請記住,我給了你一段代碼。一切都被宣佈。 – Zack

+0

嗯,這裏只有兩種可能性:businessNames對象爲null或業務對象爲null。用斷點和調試透視圖或日誌語句來檢查它是哪一個。 – FWeigl

+0

這是在抱怨我沒有正確啓動我的數組列表,即'列表 businessNames = new ArrayList ();'。所以這是固定的,但現在它抱怨我的hashmap沒有正確啓動。 'HashMap > listDataChild;'我該怎麼做?查看我在代碼中添加的新循環 – Zack