I'm trying to retrive the data of the last-child in my Firebase Database.
.
What I would like to do is to add another Records child, that is called like the last-child id-number, increased by 1. So in this specific case, for example, I want to create a child called "3", with all its relative fields (id, months, name, time).
I've tried with the following code, but when I run it, it never enters in the addListenerForSingleValueEvent(), and I don't know why. It doesn't give me any error, but it simply doesn't enter in the method.
String name = Joe;
int months= 5;
long time = 10;
FirebaseDatabase database = FirebaseDatabase.getInstance("db-uri");
DatabaseReference myRef = database.getReference().child("Records");
myRef.limitToLast(1).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
int last_db_user = dataSnapshot.child("id").getValue(Integer.class);
int new_user = last_db_user++;
//The User class is just a class that helps setting the values in the db
User user = new User(new_user, name, months, time);
String key = String.valueOf(new_user);
myRef.child(key).setValue(user);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
I've also tried writing it like the following but, despite the fact that this time it enters in the addListenerForSingleValueEvent(), it says that it can't execute the toInteger() method because the object is null.
myRef.child("Records").limitToLast(1).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
int last_db_user = dataSnapshot.child("id").getValue(Integer.class);
int new_user = last_db_user++;
//The User class is just a class that helps setting the values in the db
User user = new User(new_user, name, months, time);
String key = String.valueOf(new_user);
myRef.child(key).setValue(user);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
Any ideas on how can I make this work?
UPDATE
String name = Joe;
int months= 5;
long time = 10;
FirebaseDatabase database = FirebaseDatabase.getInstance("db-uri");
DatabaseReference myRef = database.getReference().child("Records");
myRef.orderByChild("id").limitToLast(1).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
int last_db_user = snapshot.child("id").getValue(Integer.class);
int new_user = last_db_user++;
//The User class is just a class that helps setting the values in the db
User user = new User(new_user, name, months, time);
String key = String.valueOf(new_user);
myRef.child(key).setValue(user);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
With this code I get the following exception:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.app, PID: 28429 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference at game.GameLogic$1.onDataChange(GameLogic.java:318) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55) at android.os.Handler.handleCallback(Handler.java:900) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8393) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)