I have been trying to run the transaction method but it fails to both get and set proper data in the Firebase Realtime Database.
Following is the code:
private void receiveMoney() {
String mobile=recipientText.getText().toString().trim();
Double amount=Double.parseDouble(amountText.getText().toString().trim());
mDatabase3=FirebaseDatabase.getInstance().getReference("account_details/Gn2eRixQQiTpvhX870n6nZp66tO2");
mDatabase3.runTransaction(new Transaction.Handler() {
@NonNull
@Override
public Transaction.Result doTransaction(@NonNull MutableData currentData) {
Double newBalance=(double) currentData.child("initialBalance").getValue();
System.out.println("Current Balance: "+newBalance+" Amount: "+amount);
currentData.setValue(newBalance);
return Transaction.success(currentData);
}
@Override
public void onComplete(@Nullable DatabaseError error, boolean committed, @Nullable DataSnapshot currentData) {
}
});
}
Following is my database: Database Screenshot
Following is the error I get in the Run log:
E/RepoOperation: Caught Throwable.
java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
at com.example.efikas.SendMoneyActivity$1.doTransaction(SendMoneyActivity.java:86)
at com.google.firebase.database.core.Repo.startTransaction(Repo.java:933)
at com.google.firebase.database.DatabaseReference$4.run(DatabaseReference.java:492)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.processTask(ThreadPoolExecutor.java:1187)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:784)
enter code here