Upload a result to firebase

0

Good I have the following code in android where it shows me how many questions have been answered well and how many have not:

private void checkResults() {
    int correctas = 0;
    int incorrectas = 0;
    int nocontestadas = 0;
    for (int i = 0; i < all_questions.length; i++) {
        if (answer_is_correct[i]) correctas++;
        else if (answer[i] == -1) nocontestadas++;
        else incorrectas++;
    }

    String message =
            String.format("Correctas: %d\nIncorrectas: %d\nNo contestadas: %d\n",
                    correctas, incorrectas, nocontestadas);



    //--cuadro de dialogo
    //construimos el constructor
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //que va a tener
    builder.setTitle(R.string.results);
    builder.setMessage(message);
    //botones
    builder.setCancelable(false);//desabilita el bton atras
    builder.setPositiveButton(R.string.finish, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent b=new Intent(getApplicationContext(),MenuActivity.class);
            startActivity(b);
            finish();
        }
    });
    //volver a empezar
    builder.setNegativeButton(R.string.start_over, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startOver();
        }
    });
    //crea el cuadro
    builder.create().show();
}

what I want to do is that the result that was obtained is saved to firebase, eh tried with the following code but it does not come out (the application stops)

String nombre = String.valueOf(correctas);
    mDataBase.child(mAuth.getCurrentUser().getUid()).child("progreso").child("suma").child("pregunta1").setValue(nombre);

it would be like this my code

private void checkResults() {
    int correctas = 0;
    int incorrectas = 0;
    int nocontestadas = 0;
    for (int i = 0; i < all_questions.length; i++) {
        if (answer_is_correct[i]) correctas++;
        else if (answer[i] == -1) nocontestadas++;
        else incorrectas++;
    }

    String message =
            String.format("Correctas: %d\nIncorrectas: %d\nNo contestadas: %d\n",
                    correctas, incorrectas, nocontestadas);



    //--cuadro de dialogo
    //construimos el constructor
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //que va a tener
    builder.setTitle(R.string.results);
    builder.setMessage(message);
    //botones
    builder.setCancelable(false);//desabilita el bton atras
    builder.setPositiveButton(R.string.finish, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent b=new Intent(getApplicationContext(),MenuActivity.class);
            startActivity(b);
            finish();
        }
    });
    //volver a empezar
    builder.setNegativeButton(R.string.start_over, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            startOver();
        }
    });
    //crea el cuadro
    builder.create().show();

// aqui implemento el codigo firebase
    String nombre = String.valueOf(correctas);
   mDataBase.child(mAuth.getCurrentUser().getUid()).child("progreso").child("suma").child("pregunta1").setValue(nombre);

}

I get the following error:

FATAL EXCEPTION: main
                                                                                        Process: com.juegosludicos.juegosludicosmejoradodos, PID: 26366
                                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
                                                                                            at com.juegosludicos.juegosludicosmejoradodos.PreguntaFacilActivity.checkResults(PreguntaFacilActivity.java:217)
                                                                                            at com.juegosludicos.juegosludicosmejoradodos.PreguntaFacilActivity.access$400(PreguntaFacilActivity.java:23)
                                                                                            at com.juegosludicos.juegosludicosmejoradodos.PreguntaFacilActivity$1.onClick(PreguntaFacilActivity.java:137)
                                                                                            at android.view.View.performClick(View.java:5246)
                                                                                            at android.widget.TextView.performClick(TextView.java:10620)
                                                                                            at android.view.View$PerformClick.run(View.java:21256)
                                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                            at android.os.Looper.loop(Looper.java:145)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                                            at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

the lines you point to is the line where you implement the firebase code

    
asked by kevvelas 14.12.2017 в 00:27
source

1 answer

0

The error is here% of%

As you specify, it makes me have your database divided so

JSLK2591LDL294K1-JKL ._ 
                       progreso._
                                 suma ._
                                       pregunta1: 

Well, the error that throws you is that you can not find the first key, I mean the user ID, to solve it there are two ways, one is enabling google sign in in authentication in firebase, if you have not done it, see if you believe the same id for the user with a setvalue before, because if you ask for the currentuser.getuserid and you do not have it created in your database it will never reach it

As the CurrentUser does not find there are several problems that can be

>        1.- Que no estes logueado con google sign in o algun sistema de logueo de firebase
>        2.- Que tus reglas de authenticacion esten mal
>        3.- Que no tengas habilitado en authentication google

PS: if you can share where you create the variable mAuth would be helpful, or tell me when your user loguea, or how your database is composed, remember that for you to get the currentuser and the Uid the user should be logged in and the mDataBase.child(mAuth.getCurrentUser().getUid()) , make a Log.ey check if mAuth.getCurrentUser() != null is different from null and returns the user's id

    
answered by 19.12.2017 в 21:39