Reputation: 57
The code gets JSON object from a webpage and makes it to a list..somewhere here an exception is arising..Please help me...
import android.content.Context;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
public class FeedActivity extends Activity implements OnFeedListener{
ListView listview;
FeedAdapter adapter;
ArrayList<Post> posts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed);
listview = (ListView)findViewById(R.id.listView);
//Data ->adapter -> ListView
adapter = new FeedAdapter(getApplicationContext(),R.layout.layout_feed_item);
listview.setAdapter(adapter);
FeedTask task = new FeedTask(this);
task.execute("https://public-api.wordpress.com/rest/v1.1/sites/androsiv.wordpress.com/posts/");
}
@Override
public void onFeed(JSONArray array) {
int length = array.length();
for(int i = 0; i<length; i++)
{
JSONObject object = array.optJSONObject(i);
Post post = new Post(object.optString("title"),object.optString("excerpt"),object.optString("featured_image"));
posts = new ArrayList<>();
posts.add(post);
}
adapter.addAll(posts);
adapter.notifyDataSetChanged();
}
public class FeedTask extends AsyncTask<String, Void, JSONArray>
{
private OnFeedListener listener;
public FeedTask(OnFeedListener listener)
{
this.listener = listener;
}
@Override
protected JSONArray doInBackground(String... params) {
String url = params[0];
OkHttpClient client = new OkHttpClient();
Request.Builder builder = new Request.Builder();
Request request = builder.url(url).build();
try {
Response response = client.newCall(request).execute();
String json = response.body().string();
try {
JSONObject object = new JSONObject(json);
JSONArray array = object.optJSONArray("posts");
return array;
}
catch(JSONException e)
{
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(JSONArray array) {
super.onPostExecute(array);
if(null == array)
return;
if(null != listener)
listener.onFeed(array);
}
}
public class FeedAdapter extends ArrayAdapter<Post>
{
private int resource;
public FeedAdapter(Context context, int resource) {
super(context, resource);
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(null == convertView)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(resource,null);
}
//Binding data
Post post = getItem(position);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView desc = (TextView) convertView.findViewById(R.id.description);
title.setText(post.title);
desc.setText(post.description);
return convertView;
}
}
public class Post
{
public String title;
public String description;
public String thumbnail; //url
public Post(String title, String desc, String thumbnail)
{
this.description=desc;
this.thumbnail=thumbnail;
this.title=title;
}
}
}
The log cat of the above program is given below...nullpointer exception is encountered in the program...someone please help me.. In the previous similar question,i couldnt find explanation.. Log cat: By changing the mentioned line i am getting the following EXCEPTION:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
FATAL EXCEPTION: AbstractCallbackSender
Process: jp.co.translimit.braindots, PID: 7444
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
FATAL EXCEPTION: AsyncTask #5
Process: jp.co.translimit.braindots, PID: 8498
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
Upvotes: 3
Views: 17305
Reputation: 4421
adapter.addAll(posts); Without creating a initializing you are trying to add it adapter. Here posts arraylist is null and you are filling the posts array in asynctask which was started after the above line. Remove the above line will work.
Upvotes: 0
Reputation: 38034
The issue is this statement:
adapter.addAll(posts);
At this point in your onCreate
method, posts
is not yet initialized and contains NULL
as value. You should either
posts
before using it (posts = new ArrayList<Post>()
)addAll(posts)
probably won't have any effect when posts
is empty, skip it alltogether. You're calling addAll(posts)
again anway in your onFeed
method.On a side note: I saw what you're re-initializingposts
with an empty array within a loop in your onFeed()
method. That won't do any good. You should initialize your posts
collection before the loop.
Upvotes: 3