解析Yelp API v2 JSON空指针异常

时间:2013-12-16 19:59:01

标签: java android json yelp

在Android中,我正在尝试使用Yelps API v2解析查询结果。我得到了正确的JSON,但由于某种原因,我得到了Null Pointer Exception的错误。以下是一些代码段。

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 个答案:

答案 0 :(得分:1)

我没有看到businessNames对象在代码中的任何地方声明或初始化。我打赌你在某个地方宣布它但没有初始化它。 此外,“评分”值似乎不是String而是Integer,所以不要使用.getString(),不是吗?