我有一个java.lang.NullPointerException,但我不知道如何解决。我的用户名未出现在TextView中。

public class Main extends AppCompatActivity {

  CircleImageView profilepic;
  TextView profileuser;

  FirebaseUser firebaseUser;
  DatabaseReference reference;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("");

    profilepic = findViewById(R.id.profile_image);
    profileuser = findViewById(R.id.username);

    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            profileuser.setText(user.getUsername());
            if (user.getImageURL().equals("default")){
                profilepic.setImageResource(R.mipmap.ic_launcher);
            }else{
                Glide.with(Main.this).load(user.getImageURL()).into(profilepic);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });


日志说我在profileuser.setText(user.getUsername());上有错误,请帮助我。

这是我的logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at com.aceappantipolo.user.aceantipolo.Main$1.onDataChange(Main.java:65)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:853)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:737)

最佳答案

这通常是因为获取了错误的数据参考。这可能是问题所在:

reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());


典型的数据节点名称使用小写字母,因此您可能在这里输入错字。

或者您需要使用以下方法检查您是否具有正确的用户数据:

User user = dataSnapshot.getValue(User.class);

if(user == null) {
  // data not found
  return;
}

// check for non null user name
if(user.getUsername != null) {
  profileuser.setText(user.getUsername());
}

// check for non null image url
if(user.getImageURL() != null) {
  if (user.getImageURL().equals("default")){
    ...
  }
}

07-28 01:44