I'm trying to make a counter, in fact I already used this idea to tell in firebase, which is a correct and safe way to do a counter for firebase.
MutableData mData;
//Contador +1 ao Firebase ao clicar na imageButton de Anuncios
private void addTransOnClickCounter(DatabaseReference mDataBaseAnunciantes) {
mDataBaseAnunciantes.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(final MutableData mutableData) {
if (integridadeAnuncios) {
//O if acima verifica a integridade dos anuncios
if (ad == null) {
return com.google.firebase.database.Transaction.success(mutableData);
}
final DatabaseReference mRef = LibraryIO.getFirebaseRef().child("anunciantes").child("estatisticas");
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if(mutableData.getValue() != null) {
int statisticCont = Integer.parseInt(String.valueOf(mutableData.child("estatisticas").child(adNomes[sorteio[contAnuncios]]).getValue())) + 1;
mutableData.child("estatisticas").child(adNomes[sorteio[contAnuncios]]).setValue(statisticCont);
}else {
mRef.child(adNomes[sorteio[contAnuncios]]).setValue(0);
if(mutableData.getValue() != null) {
Log.i("Estatistica", String.valueOf(mutableData.child("estatisticas").child(adNomes[sorteio[contAnuncios]]).getValue()));
int statisticCont = Integer.parseInt(String.valueOf(mutableData.child("estatisticas").child(adNomes[sorteio[contAnuncios]]).getValue())) + 1;
mutableData.child("estatisticas").child(adNomes[sorteio[contAnuncios]]).setValue(statisticCont);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
return com.google.firebase.database.Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
The location of the error is on the line:
int statisticCont = Integer.parseInt(String.valueOf(mutableData.child("estatisticas").child(adNomes[sorteio[contAnuncios]]).getValue())) + 1;
The following error log is occurring:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.edesonabizerril.newintercampi, PID: 22791
java.lang.NumberFormatException: For input string: "null"
at java.lang.Integer.parseInt(Integer.java:521)
at java.lang.Integer.parseInt(Integer.java:556)
at com.edesonabizerril.newintercampi.fragments.Main_Home_frag$11$1.onDataChange(Main_Home_frag.java:362)
at com.google.firebase.database.zzp.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzejp.zza(Unknown Source)
at com.google.android.gms.internal.zzelk.zzcal(Unknown Source)
at com.google.android.gms.internal.zzelq.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6267)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)