Skip to content

Combining Retrofit2 with JSON inside an Adapter with a Different Number of Items

I am trying to get some data from a rest API, but I have a problem because I have some objects with a different numbers of items.

For example:

[{7 items}, {7 items}, {6 items}...{1 item}]

They have the same key, but the problem is in some objects, one or more arguments can be missing.

My app starts running, but when I try to scroll down it crashes. I have am receiving this error:

Attempt to read from field 'android.widget.TextView com.example.covidapp.covidAdapter$ViewHolder.tv_activeCases' on a null object reference. 

Here is my MainActivity and ListView:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private ListView listView;
    private covidAdapter covidAdapter;
    private CovidApi covidApi;

    protected void onCreate(Bundle savedInstanceState) {

        listView = (ListView) findViewById(;

        Gson gson = new GsonBuilder().serializeNulls().create();

        Retrofit retrofit = new Retrofit.Builder().baseUrl("")

        covidApi = retrofit.create(CovidApi.class);

        Call<List<covid>> call = covidApi.getInfo();

            call.enqueue(new Callback<List<covid>>() {

            public void onResponse(Call<List<covid>> call, Response<List<covid>> response) {

                if (!response.isSuccessful()){

                List<covid> covids = response.body();

                covidAdapter = new covidAdapter((ArrayList<covid>) covids, getApplicationContext());


            public void onFailure(Call<List<covid>> call, Throwable t) {

This is the Adapter class:

public class covidAdapter extends BaseAdapter {

    private ArrayList<covid> covidList;
    private Context context;

    public covidAdapter(ArrayList<covid> covidList, Context context){
        this.covidList = covidList;
        this.context = context;

    public int getCount() {
        return this.covidList.size();

    public Object getItem(int i) {
        return this.covidList.get(i);

    public long getItemId(int i) {
        return i;

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

        ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.template, null);

            holder = new ViewHolder();

            holder.tv_activeCases =(TextView)convertView.findViewById(;
            holder.tv_country =(TextView)convertView.findViewById(;
            //holder.tv_lastUpdate =(TextView)convertView.findViewById(;
            holder.tv_newCases =(TextView)convertView.findViewById(;
            holder.tv_newDeaths =(TextView)convertView.findViewById(;
            holder.tv_totalCases =(TextView)convertView.findViewById(;
            holder.tv_totalDeaths =(TextView)convertView.findViewById(;
            holder.tv_totalRecovered =(TextView)convertView.findViewById(;

        } else {
            holder = (ViewHolder) convertView.getTag();

        covid covid = covidList.get(position);

        if ( covidList.size() < 0){
            System.out.println("No dps key");

        }else {

            holder.tv_activeCases.setText("Active Cases: " + covid.getActive());
            holder.tv_country.setText("Country: " + covid.getCountry());
            // holder.tv_lastUpdate.setText("Last Update: " + covid.getLastUpdate());
            holder.tv_newDeaths.setText("New Deaths: " + covid.getNewDeaths());
            holder.tv_newCases.setText("New Cases: " + covid.getNewCases());
            holder.tv_totalRecovered.setText("Recovered: " + covid.getRecovered());
            holder.tv_totalDeaths.setText("Deaths: " + covid.getTotalDeaths());
            holder.tv_totalCases.setText("Total Cases: " + covid.getTotalCases());
        return convertView;


    private static class ViewHolder{
        public TextView tv_country, tv_activeCases,  tv_newCases, tv_newDeaths, tv_totalCases, tv_totalDeaths, tv_totalRecovered;

I would appreciate any help.


To store the holder so that it can be retrieved when getView() is called again: