Counter (Transaction) in firebase fatal error

Asked

Viewed 93 times

0

Guys I’m trying to make an accountant, in fact I’ve used this idea to tell in firebase, which is a correct and safe way to make 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 error location 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)

1 answer

0

I managed to develop the solution, below:

private DatabaseReference mDBAnunciantes = LibraryIO.getFirebaseRef().child("anunciantes").child("estatisticas");
    private Integer currentValue;
    private 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(MutableData mutableData) {
                mData = mutableData;

                if (integridadeAnuncios) {

                    //O if acima verifica a integridade dos anuncios
                    if (mData == null) {
                        return Transaction.success(mutableData);
                    }


                    currentValue = mData.child(adNomes[sorteio[contAnuncios]]).getValue(Integer.class);
                    if (currentValue != null) { //Se o nó existir, aplica +1
                        mData.child(adNomes[sorteio[contAnuncios]]).setValue(currentValue + 1);
                    } else { // Se não, cria o nó em 1
                        if(!mData.hasChild(adNomes[sorteio[contAnuncios]]))
                            mDBAnunciantes.child(adNomes[sorteio[contAnuncios]]).setValue(1);
                    }
                }

                return Transaction.success(mutableData);
            }

            @Override
            public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {

            }

        });
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.