I’ve a custom adapter for my ListView. It gives me NullPointerException at the setAdapter. I’ve gone through my code several times and couldn’t figure out what is causing NPE. Following are the relevant code snippets:
Activity:
ListView mListView;
Vector<DashboardBean> dashboardBean= new Vector<DashboardBean>();
MyCustomAdapter adapter;
adapter = new MyCustomAdapter(MyDashboardActivity.this,
R.layout.testdashboard_row, dashboardBean);
adapter.clear();
dashboardBean.add(new DashboardBean(projects, workRequests, status));
mListView.setAdapter(adapter);
Dashboardbean:
public class DashboardBean {
public String project;
public String workRequest;
public String startDate;
public String status;
public String actualHours;
public String actualMins;
public DashboardBean(String project, String workRequest,String status)
{
this.project = project;
this.workRequest = workRequest;
this.status = status;
}
}
CustomAdapter:
public class MyCustomAdapter extends ArrayAdapter<DashboardBean> {
Context context;
int layoutResourceId;
DashboardBean currentMRB;
Vector<DashboardBean> data;
public MyCustomAdapter(Context context, int layoutResourceId, Vector<DashboardBean> data)
{
super(context,layoutResourceId,data);
this.layoutResourceId = layoutResourceId;
this.context=context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyStringReaderHolder holder;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);
holder= new MyStringReaderHolder();
holder.project = (TextView)row.findViewById(R.id.project);
holder.workRequest = (TextView)row.findViewById(R.id.work_request);
holder.status = (TextView) row.findViewById(R.id.status);
row.setTag(holder);
}
else
{
holder=(MyStringReaderHolder) row.getTag();
}
DashboardBean mrb = data.elementAt(position);
holder.project.setText(mrb.project);
holder.workRequest.setText(mrb.workRequest);
holder.status.setText(mrb.status);
return row;
}
static class MyStringReaderHolder
{
TextView project, workRequest, startDate, status, actualHours;
}
}
Could anyone help?
In your ‘Activity’ snippet, you never assign a value to
mListView:Because of this, it is null, and will always throw an
NullPointerException. Perhaps you have something defined in xml, and you want to be callingfindViewById(int).