-2

I'm getting a null pointer exception in the getView() method in the ContactinfoAdapter class. The offending line is:

busViewHolder.txt1.setText(c.getId());

How can to resolve it? I'm posting both Contactinfo and ContactinfoAdapter classes.

Here I'm trying to get images in getView() method.

/busViewHolder.imageView.setImageResource(c.getPhotourl());
        //busViewHolder.txt3.setText("Gender =>"+c.getGender());
        //busViewHolder.imageView.setImageLevel(c.getPhotourl());

Here is my url api links with image and without image.

https://api.myjson.com/bins/a749 without image json data

https://api.myjson.com/bins/1d3ei7 with image json data

/**
 * Created by offline on 8/12/2017.
 */

public class Contactinfo {
    private int id;
    private String first_name;
    private String gender;

    public Contactinfo() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

Adapterclass contactInfo.java

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;



/**
 * Created by offline on 8/12/2017.
 */


public class ContactinfoAdapter extends ArrayAdapter <Contactinfo>{

    private List<Contactinfo> contactinfo;
    private int resource;
    private Context context;

    public ContactinfoAdapter(Context context, int resource, List<Contactinfo> contactinfo) {

        super(context, resource, contactinfo);
        this.context=context;
        this.contactinfo=contactinfo;
        this.resource=resource;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // View v = super.getView(position, convertView, parent);
        View v = convertView;
        TextView txt1;
        TextView txt2;
        TextView txt3;
       // ImageView imageView;


        BusViewHolder busViewHolder;
        if (v == null) {
            v = LayoutInflater.from(getContext()).inflate(R.layout.test, null);
            v=LayoutInflater.from(context).inflate(resource,parent,false);
            txt1 = (TextView) v.findViewById(R.id.id1);
            txt2 = (TextView) v.findViewById(R.id.name);
            txt3 = (TextView) v.findViewById(R.id.gender);
            //imageView=(ImageView) v.findViewById(R.id.image);


            busViewHolder = new BusViewHolder(txt1,txt2,txt3);
            v.setTag(busViewHolder);
        }


        Contactinfo c = getItem(position);


        busViewHolder = (BusViewHolder) v.getTag();





          busViewHolder.txt1.setText(c.getId());

//
  //      busViewHolder.txt1.setText(c.getId());
    //    busViewHolder.txt2.setText( c.getGender());
      //  busViewHolder.txt3.setText(c.getFirst_name());
       // busViewHolder.imageView.setImageDrawable(Drawable.createFromPath("photourl"));
       // busViewHolder.imageView.setImageResource(c.getPhotourl("aqw"));
        //busViewHolder.imageView.setImageResource(c.getPhotourl());
        //busViewHolder.txt3.setText("Gender =>"+c.getGender());
        //busViewHolder.imageView.setImageLevel(c.getPhotourl());


        return v;
    }

    class BusViewHolder {
        TextView txt1;
        TextView txt2;
        TextView txt3;
       // ImageView imageView;
       // RatingBar rb;


        public BusViewHolder(TextView txt1, TextView txt2, TextView txt3) {
            this.txt1 = txt1;
            this.txt2 = txt2;
            this.txt3 = txt3;
            //this.imageView = imageView;
        }
    }

}

MainActivity.java

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         lv=(ListView)findViewById(R.id.lv);


        new Connection().execute("https://api.myjson.com/bins/a7491");




    }

    class Connection extends AsyncTask<String,Void,String>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            URL url=null;
            try {
                url=new URL(params[0]);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            try {
                HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
                InputStream inputStream=urlConnection.getInputStream();

                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                String s=bufferedReader.readLine();
                bufferedReader.close();

                return s;


            } catch (IOException e) {
                Log.e("Error",e.getMessage(),e);
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            JSONObject jsonObject=null;
            try {


                ArrayList<Contactinfo> contactinfos=new ArrayList<>();    //1

                JSONArray jsonArray=new JSONArray(s);
              jsonObject=jsonArray.getJSONObject(0);

                for(int i=0;i<jsonArray.length();i++){
                    JSONObject object=jsonArray.getJSONObject(i);

                     Contactinfo contactinfo=new Contactinfo();         //2

                    contactinfo.setId(object.getInt("id"));                         //3
                    contactinfo.setFirst_name(object.getString("first_name"));
                    contactinfo.setGender(object.getString("gender"));
                    //  contactinfo.setPhotourl(object.getString("photourl"));

                    contactinfos.add(contactinfo);                              //4
                }

                ContactinfoAdapter contactinfoAdapter=new ContactinfoAdapter(MainActivity.this,R.layout.test,contactinfos);

                lv.setAdapter(contactinfoAdapter);





            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
Kanishka
  • 403
  • 1
  • 5
  • 18
code
  • 11
  • 4

1 Answers1

1

please update your getView() method in adapter

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

    final BusViewHolder busViewHolder;
    Contactinfo c = contactinfo.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        busViewHolder = new BusViewHolder();
            busViewHolder.txt1 = (TextView)convertView.findViewById(R.id.id1);
            busViewHolder.txt2 = (TextView)convertView.findViewById(R.id.name);
            busViewHolder.txt3 = (TextView)convertView.findViewById(R.id.gender);
        convertView.setTag(busViewHolder);
    } else {
        busViewHolder = (BusViewHolder) rowView.getTag();
    }

    busViewHolder.txt1.setText("" + c.getId());
    busViewHolder.txt2.setText(c.getFirst_name());
    busViewHolder.txt3.setText(c.getGender());

    return convertView;
} // close getView

Added Method in Class

public class Contactinfo {
        private int id;
        private String first_name;
        private String gender;
        private String imageUrl;

        public Contactinfo() {

        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getFirst_name() {
            return first_name;
        }

        public void setFirst_name(String first_name) {
            this.first_name = first_name;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }
        public String getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }
    } 

Loop in onPostExecute()

for(int i = 0; i < jsonArray.length(); i++)
{
    JSONObject object = jsonArray.getJSONObject(i);
    Contactinfo contactinfo = new Contactinfo();         
    contactinfo.setId(object.getInt("id"));                         
    contactinfo.setFirst_name(object.getString("first_name"));
    contactinfo.setGender(object.getString("gender"));
    contactinfo.setImageUrl(object.getString("photourl"));
    contactinfos.add(contactinfo);                              
}
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28