0

I am trying to add a child and value in a parent in a firebase database but for some reason, after I add a data the app crashes. Can someone help me? This is my database:

Database

I have done the adding part which looks like this:

Addding

But after I click a button which will go back to the menu it crashes.

This is the code I have done:

public class Payment extends AppCompatActivity {

private static final String TAG = "AddToDatabase";

private RecyclerView mRecyclerView2;
private ViewHolder2 mAdapter2;
private DatabaseReference mDatabaseReference2;
private FirebaseAuth.AuthStateListener mAuthListener;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
private List<Model2> mModel2;
private FirebaseAuth mAuth;
private TextView Total;

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


    mRecyclerView2 = findViewById(R.id.recyclerview3);
    mRecyclerView2.setHasFixedSize(true);
    mRecyclerView2.setLayoutManager(new LinearLayoutManager(this));

    mModel2 = new ArrayList<>();
    mAuth = FirebaseAuth.getInstance();
    FirebaseUser user = mAuth.getCurrentUser();
    String userID = user.getUid();
    mAdapter2 = new ViewHolder2(Payment.this, mModel2);
    mRecyclerView2.setAdapter(mAdapter2);
    mDatabaseReference2 = FirebaseDatabase.getInstance().getReference(userID).child("Ordered");
    mDatabaseReference2.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            double cost = 0;
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Model2 model2 = postSnapshot.getValue(Model2.class);
                String stringValue = model2.getPrice();
                double price = Double.parseDouble(stringValue);

                cost = price + cost;
                String finaltotal = String.valueOf(cost);
                Total = findViewById(R.id.totalprice);
                Total.setText("Total Cost:" + finaltotal);

                mModel2.add(model2);
            }
            mAdapter2.notifyDataSetChanged();

        }

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

        }
    });

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference();

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
        }
    };
    ///progressBar = findViewById(R.id.progressbar);

    // Read from the database
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Object value = dataSnapshot.getValue();
            Log.d(TAG,"Value is"+value);
        }

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

        }
    });


}

public void onPayButton(View view) {

    //String newClick = Integer.toString(click1);
    FirebaseUser user = mAuth.getCurrentUser();
    String userID = user.getUid();
    myRef.child(userID).child("Ordered").child("5").child("food").setValue("Order Processing....");
    Intent intent = new Intent(Payment.this,Placeorder.class);
    startActivity(intent);
}

}

And this is the error message:

Error.

Can someone help me pls?

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • Please don't post screenshots of error messages or other textual content, as that makes it harder to search for them. Instead put the information into your question as plain text. The the actual problem: it looks like you're calling `toString` on a `null` value on line 62, which most likely means that `dataSnapshot` is null. See https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen May 25 '19 at 21:44
  • @FrankvanPuffelen Could you show me how to fix it please? – Damian Portaliga May 26 '19 at 18:23

0 Answers0