Skip to content
Advertisement

Firebase Database gives null values

This is my Firebase Database snippet

Database Snippet

I am trying to get the name of the expense and the expense amount and set it into a RecyclerView.

This is my adapter for the RecyclerView:

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

    Context context;
    ArrayList<Expense> list;

    public ExpenseAdapter(Context context, ArrayList<Expense> list) {
        this.context = context;
        this.list = list;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ExpenseAdapter.MyViewHolder holder, int position) {
        Expense expense = list.get(position);
        holder.expenseNameText.setText(expense.getName());
        holder.expenseAmountText.setText(expense.getAmount());
        holder.expenseAmountSign.setText(expense.getType());
//        String type = expense.getType();
//        if(type.equals("Credit")) {
//            holder.expenseAmountSign.setText("+ ");
//            holder.expenseAmountSign.setTextColor(Color.GREEN);
//        } else if(type.equals("Debit")) {
//            holder.expenseAmountSign.setText("- ");
//            holder.expenseAmountSign.setTextColor(Color.RED);
//        }
    }

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

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView expenseNameText, expenseAmountText, expenseAmountSign;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            expenseNameText = itemView.findViewById(R.id.expenseNameText);
            expenseAmountText = itemView.findViewById(R.id.expenseAmountText);
            expenseAmountSign = itemView.findViewById(R.id.expenseAmountSign);

        }
    }

}

This is my POJO class for Expense Model

package com.inamul.ledgers;

public class Expense {

    String name, amount, type;

    public Expense() {  }

    public Expense(String name, String amount, String type) {
        this.name = name;
        this.amount = amount;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "Expense{" +
                "name='" + name + ''' +
                ", amount='" + amount + ''' +
                ", type='" + type + ''' +
                '}';
    }
}

This is my code snippet

String path = Objects.requireNonNull(mAuth.getUid()) + "/ledgers/" + ledgerName + "/expenses/";
// Database Reference
databaseReference = FirebaseDatabase.getInstance().getReference(path);
databaseReference.addValueEventListener(new ValueEventListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                list.clear();
                for(DataSnapshot dataSnapshot: snapshot.getChildren()) {
                    Expense expense = dataSnapshot.getValue(Expense.class);
                    assert expense != null;
                    Log.d("TAG", expense.toString());
                    list.add(expense);
                }
                expenseAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
});

The log shows that every value in the expense class has been set to null and the database path seems to be fine

2022-07-19 13:05:01.761 26621-26621/com.inamul.expenser D/TAG: Expense{name='null', amount='null', type='null'}
2022-07-19 13:05:01.755 26621-26621/com.inamul.expenser D/PATH: XELPjRzISJN969d0ttPQeheyG1r1/ledgers/Company/expenses/

This is the output

enter image description here

What am I doing wrong?

Advertisement

Answer

You’re getting null for each property:

D/TAG: Expense{name=’null’, amount=’null’, type=’null’}

Because in the database there is no property called name, amount, or type. So the simplest solution I can think of would be to change the Expense class declaration to:

public class Expense {
    String expenseName, expenseAmount, expenseType;

    public Expense() {  }

    public Expense(String expenseName, String expenseAmount, String expenseType) {
        this.expenseName = expenseName;
        this.expenseAmount = expenseAmount;
        this.expenseType = expenseType;
    }

    public String getExpenseName() {
        return expenseName;
    }

    public void setExpenseName(String expenseName) {
        this.expenseName = expenseName;
    }

    public String getExpenseAmount() {
        return expenseAmount;
    }

    public void setExpenseAmount(String expenseAmount) {
        this.expenseAmount = expenseAmount;
    }

    public String getExpenseType() {
        return expenseType;
    }

    public void setExpenseType(String expenseType) {
        this.expenseType = expenseType;
    }

    @Override
    public String toString() {
        return "Expense{" +
                "expenseName='" + expenseName + ''' +
                ", expenseAmount='" + expenseAmount + ''' +
                ", expenseType='" + expenseType + ''' +
                '}';
    }
}

So it can match the name in the database.

Advertisement