-1

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;
    }
}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
user3337660
  • 35
  • 1
  • 7

3 Answers3

0
you have just initialize variable not set value

it should be like this

      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;

setname(this.name)  // same apply to all other
     }
Nirav Ranpara
  • 13,753
  • 3
  • 39
  • 54
0

Try like this

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(convertView == null){

         convertView = LayoutInflater.from(context).inflate(R.layout.fragment_hospitals,parent,false);
        //convertView = LayoutInflater.inflate(R.layout.fragment_hospitals,parent,false);
    }
    Data hospital = datas.get(position);

    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;
}
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

replace this method and generate getters and setters in your Data class.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Data hospital = (Data)datas.get(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.getName());

    Glide
            .with(context)
            .load(hospital.url_image)
            .centerCrop()
            .crossFade()
            .into(pilau);

    return convertView;
}
anonymous
  • 401
  • 7
  • 15