Error android Studio + Firebase

Asked

Viewed 280 times

1

I developed an application was registering and everything, but at the time I implemented the function to display the data in a Logcat, my application even stopped starting Activity. Has anyone ever had this mistake? I appreciate the help since.

Displays the following error

FATAL EXCEPTION: main Process: com.example.Matheus.kypy, PID: 9373 java.lang.Runtimeexception: Unable to start Activity Componentinfo{com.example.Matheus.kypy/com.example.Matheus.kypy.Main}: java.lang.Nullpointerexception: Attempt to invoke virtual method 'com.google.firebase.database.Databasereference com.google.firebase.database.Firebasedatabase.getReference(java.lang.String)' on a null Object Reference at android.app.Activitythread.performLaunchActivity(Activitythread.java:2325) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2387) at android.app.Activitythread.access$800(Activitythread.java:151) at android.app.Activitythread$H.handleMessage(Activitythread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.Activitythread.main(Activitythread.java:5254) 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:903) at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:698) Caused by: java.lang.Nullpointerexception: Attempt to invoke virtual method 'com.google.firebase.database.Databasereference com.google.firebase.database.Firebasedatabase.getReference(java.lang.String)' on a null Object Reference at com.example.Matheus.kypy.Main.onCreate(Main.java:54) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.Activitythread.performLaunchActivity(Activitythread.java:2278) at android.app.Activitythread.handleLaunchActivity(Activitythread.java:2387)  at android.app.Activitythread.access$800(Activitythread.java:151)  at android.app.Activitythread$H.handleMessage(Activitythread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.Activitythread.main(Activitythread.java:5254)  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:903)  at com.android.Internal.os.Zygoteinit.main(Zygoteinit.java:698)

 

Project classes:

public class Main extends AppCompatActivity {
    private EditText etNome, etData, etRg, etCpf, etEndereco, etDoenca, etProfissao;
    private TextView textViewInfo;
    private Button buttonCadastro, buttonListar,buttonVoltar;
    private FirebaseDatabase BD;
    private DatabaseReference bdR,myRef;//bdR = Banco de Dados Referencia
    private Usuarios novoUsuario;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 //Parte do Layout {
    //Botões
    buttonCadastro = (Button) findViewById(R.id.buttonCadastro);
    buttonListar = (Button) findViewById(R.id.buttonListar);
    buttonVoltar = (Button) findViewById(R.id.buttonVoltar);
    //EditTexts
    etNome = (EditText) findViewById(R.id.etNome);
    etData = (EditText) findViewById(R.id.etData);
    etRg = (EditText) findViewById(R.id.etRg);
    etCpf = (EditText) findViewById(R.id.etCpf);
    etEndereco = (EditText) findViewById(R.id.etEndereco);
    etDoenca = (EditText) findViewById(R.id.etDoenca);
    etProfissao = (EditText) findViewById(R.id.etProfissao);
    //TextViews
    textViewInfo = (TextView) findViewById(R.id.textViewInfo);
 //Parte do Layout }
    //-----------------------------------------------------------
    //BD
    bdR = FirebaseDatabase.getInstance().getReference();
    myRef = BD.getReference("-KSXUo45LKXpWMOX-BfY");

    //-----------------------------------------------------------

    buttonCadastro.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            novoUsuario = new Usuarios(
                    etNome.getText().toString(),
                    etData.getText().toString(),
                    etRg.getText().toString(),
                    etCpf.getText().toString(),
                    etEndereco.getText().toString(),
                    etDoenca.getText().toString(),
                    etProfissao.getText().toString());

            bdR.push().setValue(novoUsuario);

        }

    });

    buttonListar.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
          myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                 Usuarios u = dataSnapshot.getValue(Usuarios.class);
                     Log.e("Meu: Nome", u.getNome());
                     Log.e("Meu: Data", u.getData());
                     Log.e("Meu: RG", u.getRg());
                     Log.e("Meu: CPF", u.getCpf());
                     Log.e("Meu: Endereço", u.getEndereco());
                     Log.e("Meu: Doença", u.getDoenca());
                     Log.e("Meu: Profissão", u.getProfissao());

                 }


                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    });
}


public void abreInfo(View view){
    Intent intent = new Intent(this,Usuarios.class);
   startActivity(intent);
    setContentView(R.layout.activity_info);
}

}

User given:

public class Usuarios {

private String Nome;
private String Data;
private String Rg;
private String Cpf;
private String Endereco;
private String Doenca;
private String Profissao;

public Usuarios(String Nome, String Data, String Rg, String Cpf, String Endereco, String Doenca, String Profissao) {
    this.Nome = Nome;
    this.Data = Data;
    this.Rg = Rg;
    this.Cpf = Cpf;
    this.Endereco = Endereco;
    this.Doenca = Doenca;
    this.Profissao = Profissao;
}
public Usuarios() {

}

1 answer

1

This is because you are accessing the BD variable before it is initialized on this line:

myRef = BD.getReference("-KSXUo45LKXpWMOX-BfY");

Initialize the variable before using it:

    //BD
    BD = FirebaseDatabase.getInstance();
    bdR = BD.getReference();
    myRef = BD.getReference("-KSXUo45LKXpWMOX-BfY");

Browser other questions tagged

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