Edittext.gettext on a null Object Reference

Asked

Viewed 117 times

0

I am trying to add records in the Firebase Realtime Database, through a Alertdialog, but the Edittext components do not let me abstract the component information.

Below is the section of java..

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 Logcat of error

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)

xml layout is all right, both type and id.

2 answers

1

Dude, I’m finding it strange the edittext being instantiated at the click of the button (Edittext name... name = findBiewById(...)). The conventional is to instantiate each view in onCreate.

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

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

  • 1

    Yes, this chunk is inside onCreate. even creating at the beginning of onCreate (outside the button) gives this problem. and regarding casting is not required to use, but tried with it tbm and was not.

  • Are these views from the layout being inflated? This layout being inflated is the same as used in Mainacitivity?

  • no, I created an XML layout only pro Fab in the case

  • It would look like you’re posting the Mainactivity layout?

  • Not right now, but. I’ll post as soon as I can, I can say it’s just a Listview with Fab in the corner.

  • Quiet. Humm, so this edittext is in another layout that is not invoked by Mainactivity? Or is it in the Adapter layout in Listview?

  • it is inflated in the onClick of Fab

  • Yes, the onclick of Fab inflates a Alertdialog with a custom layout, there in the setPositiveButton method I try to string the values of the components and send to firebase.

Show 3 more comments

0

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

Just create a variable of your View customized that was passing to the AlertDialog and use the findViewById this reference created and not the MainActivity

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();
        }
    });
  • If you keep giving this error, check your Ids in the XML layout/fab_record.xml if they are all there

Browser other questions tagged

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