I'm trying to teach myself to make android apps, therefore i'm trying to create an app that shows images from an tumblr account in an list view.
I have some problem parsing the JSONObject and my app crashes because of an nullPointerException.
My code looks like this:
public class Example extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<Tweet> tweets;
try {
tweets = getTweets();
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new UserItemAdapter(this, R.layout.listitem,
tweets));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int imageViewResourceId,
ArrayList<Tweet> tweets) {
super(context, imageViewResourceId, tweets);
this.tweets = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
ImageView image = (ImageView) v.findViewById(R.id.avatar);
if (image != null) {
image.setImageBitmap(getBitmap(tweet.image_url));
}
}
return v;
}
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (Exception ex) {
return null;
}
}
public ArrayList<Tweet> getTweets() throws ClientProtocolException,
IOException, JSONException {
String searchUrl = "http://api.tumblr.com/v2/blog/www.richkidsofinstagram.tumblr.com/posts?api_key=API_KEY";
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try {
responseBody = client.execute(get, responseHandler);
} catch (Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = new JSONObject(responseBody);
JSONArray arr = null;
try {
arr = jsonObject.getJSONArray("results");
} catch (Exception ex) {
Log.v("TEST", "Exception: " + ex.getMessage());
}
for (int i = 0; i < arr.length(); i++) {
Tweet tweet = new Tweet(arr.getJSONObject(i).getString("photos"));
tweets.add(tweet);
}
return tweets;
}
public class Tweet {
public String image_url;
public Tweet(String url) {
this.image_url = url;
}
}
}
The nullPointerException occurs at line 124:
for (int i = 0; i < arr.length(); i++) {
Tweet tweet = new Tweet(arr.getJSONObject(i).getString("photos"));
tweets.add(tweet);
}
Also i have validated the address via www.jsonlint.com and the JSON looks like this:
{
"meta": {
"status": 200,
"msg": "OK"
},
"response": {
"blog": {
"title": "Rich Kids Of Instagram",
"posts": 154,
"name": "richkidsofinstagram",
"url": "http://richkidsofinstagram.tumblr.com/",
"updated": 1346803265,
"description": "They have more money than you and this is what they do.
"ask": true,
"ask_anon": true
},
"posts": [
{
"blog_name": "richkidsofinstagram",
"id": 30900248446,
"post_url": "http://richkidsofinstagram.tumblr.com/post/30900248446/hamptons-are-good",
"slug": "hamptons-are-good",
"type": "photo",
"date": "2012-09-04 23:58:45 GMT",
"timestamp": 1346803125,
"state": "published",
"format": "html",
"reblog_key": "2KosMjea",
"tags": [
"pool",
"hamptons",
"summer",
"rich",
"wealth"
],
"highlighted": [],
"note_count": 99,
"caption": "<p><span>The Hamptons are…….. good. by matthewmorton</span></p>",
"photos": [
{
"caption": "",
"alt_sizes": [
{
"width": 500,
"height": 500,
"url": "http://24.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_500.jpg"
},
{
"width": 400,
"height": 400,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_400.jpg"
},
{
"width": 250,
"height": 250,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_250.jpg"
},
{
"width": 100,
"height": 100,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_100.jpg"
},
{
"width": 75,
"height": 75,
"url": "http://25.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_75sq.jpg"
}
],
"original_size": {
"width": 500,
"height": 500,
"url": "http://24.media.tumblr.com/tumblr_m9uiqy9Mi71rb86ldo1_500.jpg"
}
}
]
},
I think the problem is that i don't specify which picture size i want or something like that, but i have no clue how to solve the problem.
If anyone could help me solve this problem it would be highly appriciated.
Note sure which line exactly is 124, copy and pasted your code and only ended up with ~115 lines total? Anyway here goes:
for (int i = 0; i < arr.length(); i++) {
That should normally be fine since the call to set arr
should throw a JSONException
exception if there is no array named "results". You eat that exception though, and just log it, which could leave arr
as null
. Also, I don't see any "results" in your JSON, did you mean "response"? That is my guess if you are getting a NullPointerException
.
Could stop reading here if you want, but going to explain why I'm guessing the next 2 lines aren't 124 / aren't the one throwing the exception:
Tweet tweet = new Tweet(arr.getJSONObject(i).getString("photos"));
That looks safe becuase if arr isn't null getJSONObject()
should return something for your i
values. And getString()
would throw a JSONException
if it couldn't find "photos". (I'm not sure what that Tweet
class is exactly, so if "photos" returned null and the Tweet
constructer died on a null argument, well that could be it, but I don't know...)
tweets.add(tweet);
This looks safe too because you initialized tweets
as new ArrayList<Tweet>();
and even if tweet
was null, an ArrayList()
won't have a problem with that.