我使用Firebase在Android中编写了一个聊天应用程序。但是,当我想设置一个简单的设置器时,会导致NullPointerException。
日志:
2019-05-04 21:54:07.560 23187-23187 / com.example.chatteszt E / AndroidRuntime:FATAL EXCEPTION:main 流程:com.example.chatteszt,PID:23187 java.lang.NullPointerException:尝试在空对象引用上写入字段“ java.lang.String com.example.chatteszt.User.uid” 在com.example.chatteszt.GroupChatActivity $ 1.onDataChange(GroupChatActivity.java:112) com.google.firebase.database.Query $ 1.onDataChange(com.google.firebase:firebase-database @@ 16.1.0:183) com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database @@ 16.1.0:75) com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database @@ 16.1.0:63) com.google.firebase.database.core.view.EventRaiser $ 1.run(com.google.firebase:firebase-database @@ 16.1.0:55) 在android.os.Handler.handleCallback(Handler.java:754) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:163) 在android.app.ActivityThread.main(ActivityThread.java:6238) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:933) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
我尝试在User类中将uid设置为public并初始化User,但无济于事。
User u;
@Override
protected void onStart() {
super.onStart();
final FirebaseUser currentUser = auth.getCurrentUser();
u.setUid(currentUser.getUid());
u.setEmail(currentUser.getEmail());
database.getReference("Users").child(currentUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
u = dataSnapshot.getValue(User.class);
u.setUid("apple"); <---------- This line throw Exception
u.setUid(currentUser.getUid());
AllMethods.name = u.getName();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
messagedb = database.getReference("messages");
messagedb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Message message = dataSnapshot.getValue(Message.class);
message.setKey(dataSnapshot.getKey());
messages.add(message);
displayMessages(messages);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Message message = dataSnapshot.getValue(Message.class);
message.setKey(dataSnapshot.getKey());
List<Message> newMessages = new ArrayList<Message>();
for (Message m : messages){
if (m.getKey().equals(message.getKey())){
newMessages.add(message);
}
else{
newMessages.add(m);
}
}
messages = newMessages;
displayMessages(messages);
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
Message message = dataSnapshot.getValue(Message.class);
message.setKey(dataSnapshot.getKey());
List<Message> newMessages = new ArrayList<Message>();
for (Message m : messages){
if (!m.getKey().equals(message.getKey())){
newMessages.add(m);
}
}
messages = newMessages;
displayMessages(messages);
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
User.java
public class User {
String uid = "";
String name = "";
String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User(){}
public String getUid() {
return uid;
}
public void setUid(String uid) {
Log.d("EREDMENY", uid + " " + this.uid);
this.uid = uid;
Log.d("EREDMENY", uid + " " + this.uid);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return "User{" +
"uid='" + uid + '\''+
", name='" + name + '\''+
", email='" + email + '\''+
'}';
}
}