0

I wanted to create a ListView in a Fragment. I have created a custom adapter for a an array of objects of data type ArrayList.

But the app crashes when I launch the activity containing this Article fragment.

Error

 12-31 12:01:09.746 25620-25620/com.example.root.talaash E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.root.talaash, PID: 25620
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.root.talaash/com.example.root.talaash.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)
    at android.app.ActivityThread.access$800(ActivityThread.java:167)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5309)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
    at com.example.root.fragments.ArticleFragment.onCreateView(ArticleFragment.java:40)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
    at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
    at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
    at android.app.Activity.performStart(Activity.java:6068)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418) 
    at android.app.ActivityThread.access$800(ActivityThread.java:167) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5309) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 

Article class(package : com.example.root.aditional)

public class Article {
    String title;
    String author;
    String category;
    String date;
    String url;
    Boolean tag = false;

    public Article(String title, String author, String category, String date, String url) {
        this.title = title;
        this.author = author;
        this.category = category;
        this.date = date;
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getCategory() {
        return category;
    }

    public String getDate() {
        return date;
    }

    public void setTag(Boolean tag) {
        this.tag = tag;
    }

    public String getUrl() {
        return url;
    }
}

ArticleAdaper class(package : com.example.root.adapters)

public class ArticleAdapter<A> extends ArrayAdapter<Article>{

    private static class ViewHolder {
        TextView title, author, category, date;
    }

    public ArticleAdapter(Context context, ArrayList<Article> articles) {
        super(context, R.layout.row_article, articles);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(this.getContext()).inflate(R.layout.row_article, parent, false);

            viewHolder.title = (TextView)convertView.findViewById(R.id.titleText);
            viewHolder.author= (TextView)convertView.findViewById(R.id.authorText);
            viewHolder.category= (TextView)convertView.findViewById(R.id.categoryText);
            viewHolder.date = (TextView)convertView.findViewById(R.id.dateText);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }

        Article item = getItem(position);
        if(item!= null){
            viewHolder.title.setText(item.getTitle());
            viewHolder.author.setText(item.getAuthor());
            viewHolder.category.setText(item.getCategory());
            viewHolder.date.setText(item.getDate());
        }

        return convertView;
    }
}

I wrote the above class with the help of reading this question's first answer : How to use ArrayAdapter<myClass>

The fragment containing the listView(package : com.example.root.project)

public class ArticleFragment extends Fragment {

    public ArticleFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ArrayList<Article> articles = new ArrayList<>(6);
        articles.add(new Article("Rose", "Sample Author", "Sample category", "Sample date", "Sample url"));

        ListAdapter articleAdapter = new ArticleAdapter(this.getContext(), articles);
        ListView listView = (ListView)container.findViewById(R.id.articleListView);
        listView.setAdapter(articleAdapter);

        /*ListView listView = (ListView)findViewById(R.id.articleListView);*/
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_article, container, false);
    }
}

I am very new to android development and I would like to have a detailed explanation as to where I am going wrong and what I should do to fix it.

Community
  • 1
  • 1
Kartik
  • 1
  • 2
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – hotzst Dec 31 '16 at 07:52

4 Answers4

1

NullPointerException is thrown when an application attempts to use an object reference that has the null value.

At first return your view .

Don't

return inflater.inflate(R.layout.fragment_article, container, false);

Do

return your view ;

Then pass getActivity()

Example

   View rootView= inflater.inflate(R.layout.fragment_article, container, false);
    ListView listView = (ListView)rootView.findViewById(R.id.articleListView);
    ListAdapter articleAdapter = new ArticleAdapter(getActivity(), articles);
    listView.setAdapter(articleAdapter);

   return rootView;
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

You are trying to find the ListView inside wrong ViewGroup. Try this -

View root = inflater.inflate(R.layout.fragment_article, container, false);
ListView listView = (ListView) root.findViewById(R.id.articleListView);

return root;
jaibatrik
  • 6,770
  • 9
  • 33
  • 62
0

You need to inflate the view first in your onCreateView and then you need to find the ListView there instead of container. Return the view finally as the last return statement in your onCreateView.

public class ArticleFragment extends Fragment {

    public ArticleFragment() {
        // Required empty public constructor
    }

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

        ArrayList<Article> articles = new ArrayList<>(6);
        articles.add(new Article("Rose", "Sample Author", "Sample category", "Sample date", "Sample url"));

        ListAdapter articleAdapter = new ArticleAdapter(this.getContext(), articles);
        ListView listView = (ListView) v.findViewById(R.id.articleListView);
        listView.setAdapter(articleAdapter);

        // Return the inflated Fragment here
        return v;
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

try this:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_article, container, false);
    ArrayList<Article> articles = new ArrayList<>(6);
    articles.add(new Article("Rose", "Sample Author", "Sample category", "Sample date", "Sample url"));

    ListAdapter articleAdapter = new ArticleAdapter(this.getContext(), articles);
    ListView listView = (ListView)view.findViewById(R.id.articleListView);

    listView.setAdapter(articleAdapter);

    /*ListView listView = (ListView)findViewById(R.id.articleListView);*/
    // Inflate the layout for this fragment
    return view;
}

you are not getting the listview properly

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62