0

I have a custom listview I am using with a custom adapter. When the app opens initially the first few items are shown properly. However, when I start to scroll down, I get a nullPointerException in the getView() method:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ListViewItem item = items.get(position);

    View vi = convertView;

    if (item.Type.equals("Homework")) {

        if (convertView == null)
            vi = inflater.inflate(R.layout.item_row_homework, null);

        ImageView imgThumbnail = (ImageView) vi
                .findViewById(R.id.homework_imgThumbnail);
        TextView txtTitle = (TextView) vi
                .findViewById(R.id.homework_txtTitle);
        TextView txtSubTitle = (TextView) vi
                .findViewById(R.id.homework_txtSubTitle);

        imgThumbnail.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubTitle.setText(item.SubTitle);
    }
    if (item.Type.equals("Study")) {

        if (convertView == null)
            vi = inflater.inflate(R.layout.item_row_study, null);

        ImageView imgThumbnail = (ImageView) vi
                .findViewById(R.id.study_imgThumbnail);
        TextView txtTitle = (TextView) vi
                .findViewById(R.id.study_txtTitle);
        TextView txtSubTitle = (TextView) vi
                .findViewById(R.id.study_txtSubTitle);

        imgThumbnail.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubTitle.setText(item.SubTitle);
    }
    if (item.Type.equals("Project")) {

        if (convertView == null)
            vi = inflater.inflate(R.layout.item_row_project, null);

        ImageView imgThumbnail = (ImageView) vi
                .findViewById(R.id.project_imgThumbnail);
        TextView txtTitle = (TextView) vi
                .findViewById(R.id.project_txtTitle);
        TextView txtSubTitle = (TextView) vi
                .findViewById(R.id.project_txtSubTitle);

        imgThumbnail.setImageResource(item.ThumbnailResource);
        txtTitle.setText(item.Title);
        txtSubTitle.setText(item.SubTitle);
    }

    return vi;
}

and it comes in any of the three instances of the line

imgThumbnail.setImageResource(item.ThumbnailResource);

I have looked at other SO questions on this but have not understood exactly what is going on.

Thanks.

epsilondelta
  • 190
  • 2
  • 10
  • Did you double check that `findViewById` on `R.id.homework_imgThumbnail`, `R.id.study_imgThumbnail` and `R.id.project_imgThumbnail` doesn't return null?. – rbarriuso Jan 14 '14 at 01:15
  • Post your logcat. Check the line which logcat told you there is a NPE. – Trung Nguyen Jan 14 '14 at 03:25
  • @epsilondelta Can you please check if the question was answered? – rbarriuso Jan 16 '14 at 11:49
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Albireo Mar 07 '16 at 08:55

1 Answers1

0

I think you're trying to re-use the wrong type of view.

When you scroll down you might receive a convertView which was created with, say, the R.layout.item_row_homework layout, but the item you have to draw at position corresponds to a different type, say, "Project".

Try to use the method View.setTag to pass the view type information and check it before reusing it. In case the type is different, you'll probably need to inflate a new view.

As long as you are using a custom adapter, a better approach would be overriding the getItemViewType(int position) and getViewTypeCount() methods to define the different view types supported.

rbarriuso
  • 787
  • 8
  • 30