0

So, I'm writing this nifty custom adapter for a listView that should display profile info. Everything works well - I successfully pull out info from database, put it into ArrayList and achieve something like this in the MainActivity:

ProfileListAdapter adapter = new ProfileListAdapter(activity, R.layout.existing_profile_menu, profiles);
listView.setAdapter(adapter);

For ProfileListAdapter I use:

public class ProfileListAdapter extends ArrayAdapter<ArrayList<String>> {
    private Context context;
    private ArrayList<ArrayList<String>> values;
    private int layoutId;

    public ProfileListAdapter(Context context, int resource, ArrayList<ArrayList<String>> values)
    {
        super(context, R.layout.existing_profile_menu, values);
        this.context = context;
        this.layoutId = resource;
        this.values = values;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.existing_profile_menu, parent, false);

        TextView mainTitle = (TextView)rowView.findViewById(R.id.mainLine);
        TextView subTitle = (TextView)rowView.findViewById(R.id.subLine);

        mainTitle.setText(values.get(position).get(1) + " " + values.get(position).get(2));
        subTitle.setText(values.get(position).get(6) + " " + values.get(position).get(3));

        return rowView;
    }

}

What I get as an error is:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

And I'm pretty sure that it's somewhere at this section:

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.existing_profile_menu, parent, false);

I looked around similar sounding questions here, but couldn't find anything that would budge my case.

[UPDATE]

Issue resolved. Make sure that views are located in the layout you're using for the adapter. Or that you didn't mess up with layout naming.

Oleksandr Firsov
  • 1,428
  • 1
  • 21
  • 48
  • Post your whole logcat – Yupi Jun 10 '17 at 01:56
  • 1
    It's simple problem, just look closely into the logcat at which position the exception throw, then check to see which `TextView` is null. Then put a break point here to debug to see what cause the problem – TuyenNTA Jun 10 '17 at 02:12
  • Thanks mate! Turned out I used the wrong layout for the adapter. Noob mistake. Time to grab some shuteye. – Oleksandr Firsov Jun 10 '17 at 02:21

0 Answers0