I'm developing an application in Android Studio and using Firebase as a bank, but after finalizing the project structure when I went to test it it has an Android Strudio reference error with Firebase. I can not solve the problem, can anyone figure out how to solve it.
The error is as follows:FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matheus.cond/com.example.matheus.cond.MainActivity}: 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:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
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.cond.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
Program:
public class MainActivity extends AppCompatActivity {
EditText user_name, user_data, user_email, user_bloco,user_apto;
TextView txt_nome,txt_data,txt_email,txt_bloco,txt_apto;
Button btn_salvar;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase database;
private String userId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--Variaveis de cadastro de informações--
user_name = (EditText) findViewById(R.id.user_name);
user_data = (EditText)findViewById(R.id.user_data);
user_email = (EditText)findViewById(R.id.user_email);
user_bloco = (EditText)findViewById(R.id.user_bloco);
user_apto = (EditText)findViewById(R.id.user_apto);
//--Variaveis de exibição de informações--
txt_nome =(TextView) findViewById(R.id.txt_nome);
txt_data =(TextView) findViewById(R.id.txt_data);
txt_email =(TextView) findViewById(R.id.txt_email);
txt_bloco = (TextView) findViewById(R.id.txt_bloco);
txt_apto = (TextView) findViewById(R.id.txt_apto);
//--Botões pra cadastro--
btn_salvar = (Button) findViewById(R.id.btn_salvar);
//Action Bar
//--FireBase--
mFirebaseDatabase = database.getReference("Usuarios");
database.getReference("app_title").setValue("TopazioVille");
database.getReference("app_title").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String app_title = dataSnapshot.getValue(String.class);
getSupportActionBar().setTitle(app_title);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//--Salvar e atualizar os dados--
btn_salvar.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String nome = user_name.getText().toString();
String data = user_data.getText().toString();
String email = user_email.getText().toString();
String bloco = user_bloco.getText().toString();
String apto = user_apto.getText().toString();
if(TextUtils.isEmpty(userId)){
createUsuarios(nome,data,email,bloco,apto);
}
else
{
updateUsuarios(nome,data,email,bloco,apto);
}
}
});
toggleButton();
}
private void toggleButton(){
if(TextUtils.isEmpty(userId)){
btn_salvar.setText("Salvar");
}
else
{
btn_salvar.setText("Atualizar");
}
}
private void createUsuarios(String nome, String data, String email, String bloco, String apto){
if(TextUtils.isEmpty(userId)){
userId = mFirebaseDatabase.push().getKey();
}
Usuarios ususarios = new Usuarios(nome,data,email,bloco,apto);
mFirebaseDatabase.child(userId).setValue(ususarios);
addUsuariosChangeListener();
}
private void addUsuariosChangeListener(){
mFirebaseDatabase.child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Usuarios usuarios = dataSnapshot.getValue(Usuarios.class);
//ver se o valor é vazio
if(usuarios == null){
return;
}
txt_nome.setText(usuarios.nome);
txt_data.setText(usuarios.data);
txt_email.setText(usuarios.email);
txt_bloco.setText(usuarios.bloco);
txt_apto.setText(usuarios.apto);
//Limpar os campos
txt_nome.setText("");
txt_data.setText("");
txt_email.setText("");
txt_bloco.setText("");
txt_apto.setText("");
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void updateUsuarios(String nome, String data, String email, String bloco, String apto){
if(!TextUtils.isEmpty(nome))
mFirebaseDatabase.child(userId).child("nome").setValue(nome);
if(!TextUtils.isEmpty(nome))
mFirebaseDatabase.child(userId).child("data").setValue(data);
if(!TextUtils.isEmpty(nome))
mFirebaseDatabase.child(userId).child("email").setValue(email);
if(!TextUtils.isEmpty(nome))
mFirebaseDatabase.child(userId).child("bloco").setValue(bloco);
if(!TextUtils.isEmpty(nome))
mFirebaseDatabase.child(userId).child("apto").setValue(apto);
}
}