运行代码时,我得到一个NullPointerException
,因为我的Firestore文档不会下载到我的对象中。
我不确定为什么,但是我环顾了对象getter和setter的格式,我认为我做对了,但是错误消息仍然显示数据库中的字段没有getter或setter。
SubjectGroupPage.java
db.collection("messagesIT")
/*.orderBy("TimeSent", Query.Direction.DESCENDING)*/
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
ArrayList<MessageObj> messages = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
MessageObj mess = document.toObject(MessageObj.class);
messages.add(mess);
Log.d(TAG, "" + mess);
showToast("b");
}
//reading and displaying messages
for (int i = 0; i < messages.size(); i++) {
showToast("d");
TableLayout tl = findViewById(R.id.TableLayoutSubjectGroupPage);
TableRow trMessage = new TableRow(SubjectGroupPage.this);
TableRow trSender = new TableRow(SubjectGroupPage.this);
TextView txtSender = new TextView(SubjectGroupPage.this);
TextView txtMessage = new TextView(SubjectGroupPage.this);
txtMessage.setText(messages.get(i).getMessage());
txtSender.setText(messages.get(i).getSender());
txtMessage.setTextSize(20);
txtMessage.setTextColor(0);
txtMessage.setWidth(0);
if (messages.get(i).getSender().equals(currentUser.getUid())) {
txtMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
txtSender.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
} else {
txtMessage.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
txtSender.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
}
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 1);
txtMessage.setLayoutParams(param);
//creating message display
trSender.addView(txtSender);
trMessage.addView(txtMessage);
tl.addView(txtSender);
tl.addView(trMessage);
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
showToast("There has been an error, please try again later.");
Log.d(TAG, "Error: " + e);
}
});
LogChat
W/Firestore: (0.6.6-dev) [CustomClassMapper]: No setter/field for Sender found on class oxley.it.collectivelearning.MessageObj
W/Firestore: (0.6.6-dev) [CustomClassMapper]: No setter/field for Timesent found on class oxley.it.collectivelearning.MessageObj (fields/setters are case sensitive!)
(0.6.6-dev) [CustomClassMapper]: No setter/field for Message found on class oxley.it.collectivelearning.MessageObj
D/SubjectGroupPage.java: oxley.it.collectivelearning.MessageObj@e82129e
W/Firestore: (0.6.6-dev) [CustomClassMapper]: No setter/field for Sender found on class oxley.it.collectivelearning.MessageObj
W/Firestore: (0.6.6-dev) [CustomClassMapper]: No setter/field for TimeSent found on class oxley.it.collectivelearning.MessageObj (fields/setters are case sensitive!)
W/Firestore: (0.6.6-dev) [CustomClassMapper]: No setter/field for Timesent found on class oxley.it.collectivelearning.MessageObj (fields/setters are case sensitive!)
W/Firestore: (0.6.6-dev) [CustomClassMapper]: No setter/field for Message found on class oxley.it.collectivelearning.MessageObj
D/SubjectGroupPage.java: oxley.it.collectivelearning.MessageObj@743769b
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: oxley.it.collectivelearning, PID: 23895
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
java.lang.String.equals(java.lang.Object)' on a null object reference
at
oxley.it.collectivelearning.SubjectGroupPage$retrieveMessages$2
.onComplete(SubjectGroupPage.java:154)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:187)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller
.run(RuntimeInit.java:514)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
MessageObj.java
public class MessageObj {
private String message;
private String sender;
private Timestamp timesent;
public MessageObj() {}
public void setMessage(String message, String sender, Timestamp timesent) {
this.message = message;
}
@Exclude
public String getMessage() {
return message;
}
public void setSender(String sender) {
this.sender = sender;
}
@Exclude
public String getSender() {
return sender;
}
public void setTimesent(Timestamp timesent) {
this.timesent = timesent;
}
public Timestamp getTimesent() {
return timesent;
}
}
正常运行时,我希望ArrayList能够保存所有文档,并能够使用ArrayList上的对象获取器和设置器。
答案:
它的工作原理是将其解析为这样的对象;
for (QueryDocumentSnapshot document : task.getResult()) {
String message = document.getString("Message");
String sender = document.getString("Sender");
String timesent= document.getString("Timesent");
MessageObj mess = new MessageObj (message, sender, timesent);
messages.add(mess);
Log.d(TAG, "" + mess);
showToast("b");
}
答案 0 :(得分:1)
由于在吸气剂之前使用了@Exclude
批注,因此出现了这些错误。除此之外,setMessage()
方法应仅包含一个参数。构造MessageObj
类的正确方法是:
public class MessageObj {
private String message;
private String sender;
private Timestamp timesent;
public MessageObj() {}
public MessageObj(String message, String sender, Timestamp timesent) {
this.message = message;
this.sender = sender;
this.timesent = timesent;
}
public String getMessage() { return message; }
public String getSender() { return sender; }
public Timestamp getTimesent() { return timesent; }
}
或更简单,使用公共字段:
public class MessageObj {
public String message;
public String sender;
public Timestamp timesent;
public MessageObj() {}
public MessageObj(String message, String sender, Timestamp timesent) {
this.message = message;
this.sender = sender;
this.timesent = timesent;
}
}