-1

When my arraylist does not have an item in it, the CollectionsFragment fails to open and crashes my application.

The CollectionsFragment can only be accessed if an object has been added to it already.

package fragments;

/*Imports*/

public class CollectionsFragment extends Fragment {

private static List<Article> mArticleList = new ArrayList<>();
private ListView lvArticles;
private CollectionAdapter mAdapter;

public CollectionsFragment() {}

public static CollectionsFragment newInstance() {
    Bundle args = new Bundle();
    CollectionsFragment fragment = new CollectionsFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new MyTask().execute();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_collections, container, false);

    lvArticles = (ListView) view.findViewById(R.id.lvArticles);
    mAdapter = new CollectionAdapter(getContext(), R.layout.row, mArticleList);
    lvArticles.setAdapter(mAdapter);
    lvArticles.setEmptyView(view.findViewById(R.id.empty));

    return view;
}

private class MyTask extends AsyncTask<Article, Article, Void> {

    @Override
    protected Void doInBackground(Article... articles) {
        Article item = (Article) getArguments().getSerializable("Article");
        mArticleList.add(item);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        mAdapter.notifyDataSetChanged();

        lvArticles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Article item = mArticleList.get(position);
                Intent intent = new Intent(getContext(), WebViewActivity.class);
                intent.putExtra("URL", item.getUrl());
                getContext().startActivity(intent);
            }
        });

        lvArticles.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Article item = mArticleList.get(position);
                createDialog(view, item);
                return true;
            }
        });
    }

    private void createDialog(View view, final Article item) {

        final Dialog d = new Dialog(getContext());
        d.setContentView(R.layout.dialog_collections);
        d.setTitle("Remove article?");
        d.setCancelable(true);

        Button b = (Button) d.findViewById(R.id.button1);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mArticleList.remove(item);
                mAdapter.notifyDataSetChanged();
            }
        });

        d.show();
    }
}
}
LOG FILE
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String models.Article.getAuthor()' on a null object reference
                                                                        at adapters.CollectionAdapter.getView(CollectionAdapter.java:49)
                                                                        at android.widget.AbsListView.obtainView(AbsListView.java:2347)
                                                                        at android.widget.ListView.makeAndAddView(ListView.java:1864)
                                                                        at android.widget.ListView.fillDown(ListView.java:698)
                                                                        at android.widget.ListView.fillFromTop(ListView.java:759)
                                                                        at android.widget.ListView.layoutChildren(ListView.java:1659)
                                                                        at android.widget.AbsListView.onLayout(AbsListView.java:2151)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
                                                                        at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                        at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1367)
                                                                        at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:849)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1193)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                        at android.view.View.layout(View.java:15718)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5039)
                                                                        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2129)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1886)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1103)
                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5944)
                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:601)
                                                                        at android.view.Choreographer.doFrame(Choreographer.java:571)
                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Loo

Adapter

package adapters;

/* Imports */

public class CollectionAdapter extends ArrayAdapter {

    private List<Article> articleList;
    private int resource;
    private final LayoutInflater inflater;

    public CollectionAdapter(Context context, int resource, List<Article> objects) {
        super(context, resource, objects);
        articleList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.author = (TextView) convertView.findViewById(R.id.a_author);
            holder.title = (TextView) convertView.findViewById(R.id.a_title);
            holder.desc = (TextView) convertView.findViewById(R.id.a_desc);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.author.setText(articleList.get(position).getAuthor());
        holder.title.setText(articleList.get(position).getTitle());
        holder.desc.setText(articleList.get(position).getDescription());

        return convertView;
    }

    private class ViewHolder {
        private TextView author;
        private TextView title;
        private TextView desc;
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
chowdud
  • 43
  • 5

4 Answers4

0

Do the following:

  1. Make your View view object class field by declaring it outside onCreateView().

  2. move these lines in onPostExecute()

as there are chances that your asyncTask has not yet completed and you are trying to use the mArticleList inside your adapter.

mAdapter = new CollectionAdapter(getContext(), R.layout.row, mArticleList);
lvArticles.setAdapter(mAdapter);
lvArticles.setEmptyView(view.findViewById(R.id.empty));
Ravinder Bhandari
  • 2,546
  • 21
  • 25
0

In your AsyncTask :

@Override
    protected Void doInBackground(Article... articles) {
        Article item = (Article) getArguments().getSerializable("Article");
         if(item!=null){
            mArticleList.add(item);
        }
        return null;
    }

In your code ,ArrayList has added NULL model class so that you was getting error on line

  holder.author.setText(articleList.get(position).getAuthor());

And one more important Override Method is missing.

@Override
    public int getCount() {
        return articleList != null ? articleList.size() : 0;
    }
Mavya Soni
  • 932
  • 7
  • 15
0

I don't think your AsyncTask is necessary. Nothing in the code is blocking or a long running process.

The problem is that you're getting null objects within the list, which I assume come from getSerializable, so it appears how you started the Fragment with the Author argument is causing that issue.

Besides that, you can use mAdapter.add to add objects into the ListView, but you should have extends ArrayAdapter<Article> {

And you should set the click event listeners from onCreateView. If you did need the AsyncTask, it should only add data to the adapter and notify it, not reassign all the listeners for the ListView

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-1

Also, consider doing this:

 @Override
    public int getCount() {
        return articleList != null ? articleList.size() : 0;
    }

this will prevent your list being loaded and will you will not have null pointers.

Miljan Vulovic
  • 1,586
  • 3
  • 20
  • 48