我是Android和Firebase的新手,我试图从firease实时数据库中检索数据,但一次又一次面对java.lang.NullPointerException
btnview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
firebaseDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String firstname=dataSnapshot.child("name").getValue().toString();
String cityname=dataSnapshot.child("city").getValue().toString();
name.setText(firstname);
city.setText(cityname);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
Logcat
07-15 16:45:33.049 3537-3537/com.example.newfireabseapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.newfireabseapp, PID: 3537
java.lang.NullPointerException
at com.example.newfireabseapp.MainActivity$2$1.onDataChange(MainActivity.java:63)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5590)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)
at dalvik.system.NativeStart.main(Native Method)
这是我的数据库结构