I'm shooting data from the Firebase, but I can't add it to the list

4229 views java
0

I actually took the data and printed it with print.But when I add to the list, it gives null object exception.How can I solve the problem?I'm getting an error because I'm creating an empty object.I don't understand why.,What should I do for the solution?

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()){
                Mac mac = new Mac();
                mac.setMackodu(ds.getValue(Mac.class).getMackodu());
                mac.setTakimlar(ds.getValue(Mac.class).getTakimlar());
                mac.setTahmin(ds.getValue(Mac.class).getTahmin());
                System.out.println("macss " + mac.getMackodu());
                System.out.println("macss " + mac.getTakimlar());
                System.out.println("macss " + mac.getTahmin());
                macList.add(mac);

            }

            myRecyclerAdapter = new MyRecyclerAdapter(getContext(),macList);
            recyclerView.setAdapter(myRecyclerAdapter);
            myRecyclerAdapter.notifyDataSetChanged();

        }

public class Mac{

    private String macKodu;
    private String takimlar;
    private String tahmin;

    public Mac() {
    }

    public Mac(String macKodu, String takimlar, String tahmin) {
        this.macKodu = macKodu;
        this.takimlar = takimlar;
        this.tahmin = tahmin;
    }

    public String getMacKodu() {
        return macKodu;
    }

    public void setMacKodu(String macKodu) {
        this.macKodu = macKodu;
    }

    public String getTakimlar() {
        return takimlar;
    }

    public void setTakimlar(String takimlar) {
        this.takimlar = takimlar;
    }

    public String getTahmin() {
        return tahmin;
    }

    public void setTahmin(String tahmin) {
        this.tahmin = tahmin;
    }

    @Override
    public String toString() {
        return "Mac{" +
                "macKodu='" + macKodu + '\'' +
                ", takimlar='" + takimlar + '\'' +
                ", tahmin='" + tahmin + '\'' +
                '}';
    }
}

answered question

1 Answer

7

Put this two lines

myRecyclerAdapter = new MyRecyclerAdapter(getContext(),macList); recyclerView.setAdapter(myRecyclerAdapter);

out of

myRef.addValueEventListener

posted this

Have an answer?

JD

Please login first before posting an answer.