0

I have tried many ways to get the sum of all the amounts from all the child's but not working.

Here is my whole main activity of that particular task:

public class hostelincome extends AppCompatActivity {

   EditText name,amount,status,mod,seriesnumber;;
   Button storedata;
   TextView prevnumber;
   FirebaseDatabase firebaseDatabase;
   DatabaseReference reference,reference1;
String name1,amount1,status1,mod1,date,count1,count2,incometotal1;
double amount2,incometotal;
int total;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hostelincome);

    name = (EditText)findViewById(R.id.name);
    amount = (EditText)findViewById(R.id.amount);
    status = (EditText)findViewById(R.id.status);
    final String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    mod = (EditText)findViewById(R.id.mod);
    storedata = (Button)findViewById(R.id.storedata);
    firebaseDatabase = FirebaseDatabase.getInstance();
    date = getIntent().getStringExtra("date");

    reference = 
    firebaseDatabase.getReference("Hostel").child(date).child("Income").child(currentDateTimeString 
    );



    storedata.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            name1 = name.getText().toString();
            amount1 = amount.getText().toString();
            status1 = status.getText().toString();
            mod1 = mod.getText().toString();



            reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                   total = 0;

                    for(DataSnapshot ds : dataSnapshot.getChildren()) {
                        String amount = ds.child("amount").getValue(String.class);
                        int value = Integer.valueOf(amount);
                        total =+ value;
                    }
                }

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

                }
            });

            count1 = Integer.toString(total);

            Toast.makeText(getApplicationContext(),count1,Toast.LENGTH_LONG).show();


            if (amount1.isEmpty()){}
            else{
            amount2 = Double.parseDouble(amount1);}

            incomeinfo incomeinfo;
            incomeinfo = new incomeinfo(name1,amount1,status1,mod1);
            reference.setValue(incomeinfo);
        }
      });

     }
   }

Date is taken as a string adding integer values of day/month/year together.

When I run this program I am getting an error to add the values that the:

  Process: org.electromob.budget, PID: 21949
  java.lang.NumberFormatException: s == null
    at java.lang.Integer.parseInt(Integer.java:570)
    at java.lang.Integer.valueOf(Integer.java:794)
    at org.electromob.budget.hostelincome$1$1.onDataChange(hostelincome.java:71)
    at 
  com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase- 
  database@@19.2.0:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase- 
  database@@19.2.0:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase- 
  database@@19.2.0:55)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:7000)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)'''

my firebase info taking class

    public class incomeinfo {

    public String payeename;
    public String amount;
    public String status;
    public String mod;

    public incomeinfo() {
    }


    public incomeinfo(String payeename, String amount, String status, String mod) {
    this.payeename = payeename;
    this.amount = amount;
    this.status = status;
    this.mod = mod;
    }

    public String getPayeename() {
    return payeename;
    }

    public void setPayeename(String payeename) {
    this.payeename = payeename;
    }

    public String getAmount() {
    return amount;
    }

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

    public String getStatus() {
    return status;
    }

    public void setStatus(String status) {
    this.status = status;
    }

    public String getMod() {
    return mod;
    }

    public void setMod(String mod) {
    this.mod = mod;
    }

    }

Firebase structure is added below of my project:

Firebase storing structure picture click on the link

Can someone help me with a solution to my error?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

1

It looks like you're reading one level too low in the JSON. Your onDataChange iterates over the individual amount, mod, payeename and status properties, which then don't have an amount child anymore.

If this is indeed the case, you can fix it by attaching your listener one level higher:

reference = firebaseDatabase.getReference("Hostel").child(date).child("Income");
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • No change but the app is not crashing anymore but i am not getting the total of all amounts i am getting total as 0. – shashank reddy Nov 07 '19 at 15:53
  • Good to hear that this change fixes the error you had. For the next problem, I recommend that you set a breakpoint on `total = 0;`, run the app in the debugger, and then step through the code from there. – Frank van Puffelen Nov 07 '19 at 22:06
  • From a quick scan it seems that you're not handling the asynchronous nature of Firebase. Simply put: any code that needs the data from the database needs to be inside `onDataChange` or be called from there. For a longer explanation, see my answer here: https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Nov 07 '19 at 22:08