Question

In Android, I'm trying to parse the results of a query using Yelps API v2. I am getting correct JSON, but for some reason I am getting the error of Null Pointer Exception. Here are some code snippets.

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:

Line 125 is businessNames.add(business.getString("name")); above.

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

I logged the output of the first business JSONObject and this is the JSON output that I received...

{
   "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
}

As you can see. "name" is there, but I cannot grab it.

Was it helpful?

Solution

I don't see the businessNames object declared OR initialized anywhere in the code. I bet you declared it somewhere but didn't initialize it. Also the "rating" value doesn't seem to be a String but an Integer, so don't use .getString(), innit?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top