-1

I am trying to fetch some data from a server using Retrofit but there is something wrong with my code but I cant find it myself.

package com.example.shobkhana.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;   
import com.bumptech.glide.Glide;
import com.example.shobkhana.Activity.MainActivity;
import com.example.shobkhana.Model.Fooddata;
import com.example.shobkhana.Model.Popular;
import com.example.shobkhana.R;   
import java.util.ArrayList;
import java.util.List;
import java.util.Objects; 
import retrofit2.Callback;

public class PopularAdapter extends RecyclerView.Adapter<PopularAdapter.PopularViewHolder> {

    private  Context context;
    private List<Popular>popularlist;


    public PopularAdapter(@NonNull Context context, List<Popular> popularlist) {
        this.context =context;
        this.popularlist = popularlist;

    }

    @NonNull
    @Override
    public PopularAdapter.PopularViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view=LayoutInflater.from(context).inflate(R.layout.popular,parent,false);

        return new PopularViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PopularAdapter.PopularViewHolder holder, int position) {
        holder.poptext.setText(popularlist.get(position).getName());
        Glide.with(context).load(popularlist.get(position)).into(holder.popimg);

    }

    @Override
    public int getItemCount() {
        return popularlist.size();
    }

    public class PopularViewHolder extends RecyclerView.ViewHolder {
        TextView poptext;
        ImageView popimg;

        public PopularViewHolder(@NonNull View itemView) {
            super(itemView);
            popimg=itemView.findViewById(R.id.imageView2);
            poptext=itemView.findViewById(R.id.poptext);
        }
    }
}

Mainactivity

    package com.example.shobkhana.Activity;
    
    import android.os.Bundle;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.example.shobkhana.Adapter.PopularAdapter;
    import com.example.shobkhana.Model.Fooddata;
    import com.example.shobkhana.Model.Popular;
    import com.example.shobkhana.R;
    import com.example.shobkhana.Retrofit.ApiInterface;
    import com.example.shobkhana.Retrofit.Client;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import retrofit2.Call;
    import retrofit2.Callback;
    import retrofit2.Response;
    
    public class MainActivity extends AppCompatActivity {
    
    
        ApiInterface apiInterface;
        RecyclerView poprecycler;
        PopularAdapter popularAdapter;
        List<Popular>popularList;
    

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            apiInterface= Client.getRetrofitInstance().create(ApiInterface.class);
            Call<List<Fooddata>> call=apiInterface.getAlldata();
            call.enqueue(new Callback<List<Fooddata>>() {
                @Override
                public void onResponse(Call<List<Fooddata>> call, Response<List<Fooddata>> response) {
    
                    List<Fooddata> fooddataList=response.body();
                    getPopulardata(fooddataList.get(0).getPopular());
    
                }
    
                @Override
                public void onFailure(Call<List<Fooddata>> call, Throwable t) {
                    Toast.makeText(getApplicationContext(),"Server not responding",Toast.LENGTH_SHORT).show();
    
                }
            });
        }
    
   private void getPopulardata(List<Popular>popularList){
    
          poprecycler=findViewById(R.id.poprecycle);
          popularAdapter=new PopularAdapter(this,popularList);
          RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
          poprecycler.setLayoutManager(layoutManager);
          poprecycler.setAdapter(popularAdapter);

          }    
    }

When I try to run this I get this...

Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
    at com.example.shobkhana.Activity.MainActivity$1.onResponse(MainActivity.java:51)
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614

1 Answers1

0

The list that you are getting on onResponse is empty list

fooddataList.get(0).getPopular()

That is why when you try to get first object of fooddataList it thows null pointer exception. So, make sure you are getting a non-empty list.

Yunis Rasulzade
  • 325
  • 3
  • 12
  • the error he got `java.lang.Object java.util.List.get (int)null object reference` but if the list is empty it throws this exception `java.lang.IndexOutOfBoundsException: Index : 0, Size: 0` – Shay Kin Apr 07 '21 at 19:59
  • @ShayKin how should i code it then? – Tanvir Azadi Apr 08 '21 at 03:32