I
I
Ilya Kolpakov2020-07-11 12:10:48
Java
Ilya Kolpakov, 2020-07-11 12:10:48

Attempt to invoke virtual method '...'on a null object reference...?

As I understand it, the point is that null is returned, but I can not understand why and how to fix it. Thanks in advance

Error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.floatingactionbutton.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com .example.kivinuschat.MainActivity.onCreate(MainActivity.java:56)

Code snippet:

sendBtn = findViewById(R.id.btnSend);  
        sendBtn.setOnClickListener(new View.OnClickListener() {         //Строка 56
            @Override
            public void onClick(View view) {
                EditText textField = findViewById(R.id.messageField);
                String textEdited = textField.getText().toString();

                if(textEdited.trim()==""){
                    return; }

                FirebaseDatabase.getInstance().getReference().push().setValue(
                        new Message(
                                FirebaseAuth.getInstance().getCurrentUser().getEmail(),
                                textField.getText().toString())
                );
                textField.setText("");
                
            }
        });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-07-11
@zagayevskiy

Most likely findViewById returns null. Compare IDs. It also writes you the line number on which this happens.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question