0

I cannot show all datas to RecyclerView.I keep getting this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference at com.cdirect.weatherapp.MyLocations.onCreate(MyLocations.java:50)

MyLocations.java: 50:

rcView.setAdapter(adapter);

MyLocations.java code:

public class MyLocations extends AppCompatActivity {

BottomNavigationView altNav;
RecyclerView rcView;
ArrayList<Locations> locationsList;
DatabaseReference dbRef;
Adapter adapter;

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(MyLocations.this,MainActivity.class));
    finish();
}

@Override



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dbRef = FirebaseDatabase.getInstance().getReference("Locations");
    altNav = (BottomNavigationView) findViewById(R.id.altNav);
    rcView = findViewById(R.id.rvLocations);

    locationsList = new ArrayList<>();
    adapter = new Adapter(MyLocations.this, locationsList);

    rcView.setAdapter(adapter); //ERROR LINE
    rcView.setLayoutManager(new LinearLayoutManager(this));

Adapter.java codes:

    public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {

    ArrayList<Locations> locationsArrayList;
    Context context;

    public Adapter (Context context, ArrayList<Locations> locationsArrayList){
        this.locationsArrayList=locationsArrayList;
        this.context = context;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.recycler_view_row,parent,false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        Locations loc = locationsArrayList.get(position);
        holder.location.setText(loc.getLocation());
        holder.temperature.setText(loc.getTemp());
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView location, temperature;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            location = itemView.findViewById(R.id.tvLocation);
            temperature= itemView.findViewById(R.id.tvTemp);
        }
    }
}

Locations.java code:

    public class Locations {
    String location;
    String temp;
    String id;

    public Locations(String location, String temp, String id) {
        this.location = location;
        this.temp = temp;
        this.id = id;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public String getId() {
        return id;
    }

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

I cannot solve where is the problem. Can you help me?

  • You need to fetch data from firebase. then add it to an array. in your code, you didn't get any locations list – Daxesh V Jun 10 '22 at 11:12
  • @Cataldirect You haven't wrote code to fetch data from fireBase. See this answer https://stackoverflow.com/a/51763572/16765223 – M DEV Jun 10 '22 at 18:49

0 Answers0