EditText.getText on a null object reference

0

I'm trying to add records in the Firebase Realtime Database, through an AlertDialog, but the EditText components do not let me abstract the information from the components.

Below is the java excerpt ..

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            //Alerta para alterar informações do usuario.
            Snackbar.make(view, "Adicionando um novo registro", Snackbar.LENGTH_LONG)
                    .setAction("Registro", null).show();
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            LayoutInflater inflater = (MainActivity.this).getLayoutInflater();
            builder.setTitle("Registro");
            builder.setView(inflater.inflate(R.layout.fab_registro,null));
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    EditText data , nome , idade , horaExame , procedimento ,qtsPeliculas ;
                    nome = findViewById(R.id.registro_nome);
                    data = findViewById(R.id.registro_data);
                    horaExame = findViewById(R.id.registro_hora);
                    idade = findViewById(R.id.registro_idade);
                    qtsPeliculas = findViewById(R.id.registro_peliculas);
                    procedimento = findViewById(R.id.registro_procedimento);
                    database = FirebaseDatabase.getInstance();
                    DatabaseReference myRef = database.getReference("Paciente");
                    String child = nome.getText().toString();

                    myRef.child(child).child("hora_exame").setValue("enrique");
                    myRef.child(child).child("procedimento").setValue("toráx");
                    myRef.child(child).child("peliculas").setValue("3");

                    myRef.child(child).child("data").setValue("23456");
                    myRef.child(child).child("idade").setValue("42");
                    myRef.child(child).child("nome").setValue("enriqeu");

                }
            });
            builder.setNegativeButton("Cancelar", null);
            builder.create();
            builder.show();
        }
    });

This is the Error Logcat

Attempt to invoke virtual method 'void android.widget.EditText.getText(java.lang.CharSequence)' on a null object reference
    at com.martel.unimedradiologia_controledeestoque.MainActivity$1$1.onClick(MainActivity.java:63)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:164)

The layout xml is fine, both the type and the id.

    
asked by anonymous 26.06.2018 / 10:58

2 answers

1

Dude, I find it strange that edittext being instantiated at the click of the button (EditText name ... name = findBiewById (...)). The conventional is to instantiate each view onCreate.

Another strange thing in the code block you sent is that the casting was not done (the correct one would be: EditText name = (EditText) findViewById (...)).

Review these issues that are probably related to the cause of this problem.

    
26.06.2018 / 17:05
0

You were trying to retrieve the object in your MainActivity , but these objects were not inflated in the XML of MainActivity you have to use the reference of View inflated to retrieve objects referenced by R.id. .

You just had to create a variable of your% custom%, which was moving to View and use AlertDialog of that created reference and not findViewById

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            //Alerta para alterar informações do usuario.
            Snackbar.make(view, "Adicionando um novo registro", Snackbar.LENGTH_LONG)
                    .setAction("Registro", null).show();
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            LayoutInflater inflater = (MainActivity.this).getLayoutInflater();
            builder.setTitle("Registro");
            View view = inflater.inflate(R.layout.fab_registro,null);
            builder.setView(view);
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    EditText data , nome , idade , horaExame , procedimento ,qtsPeliculas ;
                    nome = view.findViewById<EditText>(R.id.registro_nome);
                    data = view.findViewById<EditText>(R.id.registro_data);
                    horaExame = view.findViewById<EditText>(R.id.registro_hora);
                    idade = view.findViewById<EditText>(R.id.registro_idade);
                    qtsPeliculas = view.findViewById<EditText>(R.id.registro_peliculas);
                    procedimento = view.findViewById<EditText>(R.id.registro_procedimento);
                    database = FirebaseDatabase.getInstance();
                    DatabaseReference myRef = database.getReference("Paciente");
                    String child = nome.getText().toString();

                    myRef.child(child).child("hora_exame").setValue("enrique");
                    myRef.child(child).child("procedimento").setValue("toráx");
                    myRef.child(child).child("peliculas").setValue("3");

                    myRef.child(child).child("data").setValue("23456");
                    myRef.child(child).child("idade").setValue("42");
                    myRef.child(child).child("nome").setValue("enriqeu");

                }
            });
            builder.setNegativeButton("Cancelar", null);
            builder.create();
            builder.show();
        }
    });
    
27.06.2018 / 23:57