vendettacore
vendettacore

Reputation: 1509

NullPointerException while listview fills

I have MainActivity class where I parse an HTML page with JSOUP and I also have custom adapter which store data for each row in my ListView. So I get data with JSOUP and it's ok as my LogCat tell me. But when I try to put data to my ListView I get an error.

My activity_main layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

My list_item layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/thumbImageNews"
        android:layout_marginLeft="10dp"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/titleNews"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/thumbImageNews"
        android:paddingLeft="5dp"
        android:paddingRight="0dp"
        android:paddingTop="5dp"
        android:text=""
        android:textColor="#ff0d075c"
        android:textStyle="italic"
        android:typeface="sans"
        android:gravity="center"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/date2News"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbImageNews"
        android:paddingLeft="5dp"
        android:paddingRight="0dp"
        android:paddingTop="2dp"
        android:text=""
        android:textColor="#ff585858"
        android:textSize="12sp"
        android:layout_marginLeft="10dp"/>

</RelativeLayout>

CustomListAdapter activity:

public class CustomListAdapter extends BaseAdapter {

    private ArrayList<FeedItem> listData;
    private LayoutInflater layoutInflater;
    private Context mContext;

    public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.headlineView = (TextView) convertView.findViewById(R.id.titleNews);
            holder.reportedDateView = (TextView) convertView.findViewById(R.id.date2News);
            holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImageNews);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        FeedItem newsItem = (FeedItem) listData.get(position);
        holder.headlineView.setText(newsItem.getTitle());
 //     holder.reportedDateView.setText(newsItem.getDate());

        if (holder.imageView != null) {
            new ImageDownloaderTask(holder.imageView).execute(newsItem.getAttachmentUrl());
        }

        return convertView;
    }

    static class ViewHolder {
        TextView headlineView;
        TextView reportedDateView;
        ImageView imageView;
    }
}

FeedItem class to store data for each row:

public class FeedItem implements Serializable {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    private String date;

    public String getAttachmentUrl() {
        return attachmentUrl;
    }

    public void setAttachmentUrl(String attachmentUrl) {
        this.attachmentUrl = attachmentUrl;
    }

    private String attachmentUrl;

}

And my MainActivity class where executes all main functions:

public class MainActivity extends Activity {

    public Elements title;
    public Elements picture;

    final String ATTRIBUTE_NAME_TEXT = "text";
    final String ATTRIBUTE_NAME_IMAGE = "image";
    final String ATTRIBUTE_NAME_DATE = "date";

    private ArrayList<FeedItem> feedList = null;
    private ListView feedListView = null;
    public CustomListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        feedListView = (ListView) findViewById(R.id.listView1);

        new NewThread().execute();

    }

public class NewThread extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... arg) {


    Document doc;
    try {

        doc = Jsoup.connect("http://example.com/news/").get();

        title = doc.select("h2[class=et_pt_title]");
        picture = doc.select("img");

       for (Element titles : title) {


           FeedItem item = new FeedItem();
           item.setTitle(titles.text());
           Log.w("title",""+item.getTitle());
            feedList.add(item);
        }
        for (Element img : picture){


            String iurl;
            iurl = img.absUrl("src");

            FeedItem item = new FeedItem();
            item.setAttachmentUrl(iurl);
            Log.w("imgUrl",""+item.getAttachmentUrl());
            feedList.add(item);


        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onPostExecute(String result) {

    adapter = new CustomListAdapter(MainActivity.this,feedList);
    feedListView.setAdapter(adapter);
}
}
 }

So my problem is that when feedList.add(item); executes I get NullPointerException, but Log.w("title",""+item.getTitle()); shows me that everything is ok. What I do wrong?

Upvotes: 0

Views: 78

Answers (3)

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

private ArrayList<FeedItem> feedList = null;   

without create object you are adding data in arraylist

 feedList.add(item);

please create object

feedList = new ArrayList<FeedItem>()

than Add

feedList.add(item);

Upvotes: 1

Piyush
Piyush

Reputation: 18933

Your mistake was you have just declared this

private ArrayList<FeedItem> feedList = null;

not initialized. So just initialize on onCreate() method.

feedList = new ArrayList<FeedItem> ;

Upvotes: 1

Aniruddha
Aniruddha

Reputation: 4487

Your feedList is not initialized.

So before adding items to it, please initialize.

ArrayList<FeedItem> feedList = new ArrayList<FeedItem> ;

Add the above line before you call the AsyncTask i.e. before new NewThread().execute();

It should look like this

feedList = new ArrayList<FeedItem> ;
new NewThread().execute();

Upvotes: 2

Related Questions