I need help I have been trying to find a way on how to implement custom list view and I am getting an error:-
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.edis.edis.HospitalAdaptor.getView(HospitalAdaptor.java:62)
The following is the Object that I have created and it's called Data.java
.
public class Data {
private double lat, lon;
public String name;
String groupA, groupB, groupAB,groupO;
public int url_image;
public Data(double lat, double lon, int url_image, String name, String groupA, String groupB, String groupAB, String groupO)
{
this.lat =lat;
this.lon = lon;
this.url_image =url_image;
this.name = name;
this.groupA =groupA;
this.groupB =groupB;
this.groupAB = groupAB;
this.groupO = groupO;
}
}
Then I have created an Adapter where I get an error (null object) on the line where is chakula.setText((hospital.name))
.
The following is the adapter I have used BaseAdaptor. HospitalsAdaptors.java
public class HospitalAdaptor extends BaseAdapter{
Context context;
List<Data> datas;
public HospitalAdaptor(Context context, List<Data> datas) {
this.context = context;
this.datas = datas;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Data hospital = (Data)getItem(position);
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.fragment_hospitals,parent,false);
//convertView = LayoutInflater.inflate(R.layout.fragment_hospitals,parent,false);
}
TextView chakula = (TextView)convertView.findViewById(R.id.hospitals_name);
ImageView pilau = (ImageView)convertView.findViewById(R.id.hospitals_image);
chakula.setText(hospital.name);
Glide
.with(context)
.load(hospital.url_image)
.centerCrop()
.crossFade()
.into(pilau);
return convertView;
}
}