Android Studio:尝试从 Firestore 中的文档检索数据,但返回 null

问题描述 投票:0回答:1

这是 LessonGroup 类的代码:

public class LessonGroup implements Serializable {
    //properties
    private String studentId;
    private String lessonGroupId;
    private String courseId;
    private String dayOfWeek;
    private String startTimeOfDay;
    private String endTimeOfDay;
    private ArrayList<Lesson> lessons;
    private String firstLessonDate;
    private double boughtPrice;
    private int finishedLessonCount;
    private int totalLessonCount;
    private boolean hasPaid;

    public LessonGroup(){
        //public no-argument constructor needed for FireStore database
    }

    public LessonGroup(
            String studentId,
            String lessonGroupId,
            String courseId,
            String dayOfWeek,
            String startTimeOfDay,
            String endTimeOfDay,
            String firstLessonDate,
            double boughtPrice,
            int totalLessonCount){
        //initialise private properties

        this.studentId = studentId;
        this.lessonGroupId = lessonGroupId;
        this.courseId = courseId;
        this.dayOfWeek = dayOfWeek;
        this.startTimeOfDay = startTimeOfDay;
        this.endTimeOfDay = endTimeOfDay;
        this.lessons = new ArrayList<>();
        this.firstLessonDate = firstLessonDate;
        this.boughtPrice = boughtPrice;
        this.finishedLessonCount = 0;
        this.totalLessonCount = totalLessonCount;
        this.hasPaid = false;
    }
    //various accessor methods

    public String getCourseID() {
        Log.d("Is Null", String.valueOf(courseId==null));
        return this.courseId;
    }

    public String getStudentId() {
        return studentId;
    }
    public String getLessonGroupId() {
        return lessonGroupId;
    }
    public String getFirstLessonDate() {
        return firstLessonDate;
    }
    public String getDayOfWeek() {
        return dayOfWeek;
    }
    public String getStartTimeOfDay() {
        return startTimeOfDay;
    }
    public String getEndTimeOfDay() {
        return endTimeOfDay;
    }
    public double getBoughtPrice() {
        return boughtPrice;
    }
    public ArrayList<Lesson> getLessons() {
        return lessons;
    }
    public int getRemainingLessonCount() {
        return totalLessonCount - finishedLessonCount;
    }
    public int getFinishedLessonCount() {
        return finishedLessonCount;
    }
    public boolean getHasPaid() {
        return hasPaid;
    }
}

这是我尝试调用 getCourseID() 的方法的代码:

private void getUserCoursesInfo(){
        ArrayList<LessonGroup> lessonGroups = studentUser.getAllLessonGroups();
        Log.d("StudentUser", "Is student null? " + String.valueOf(studentUser == null));
        Log.d("LessonGroup", "Is lessonGroup null? " + String.valueOf(lessonGroups == null));
        Log.d("LessonGroup", "Length " + String.valueOf(lessonGroups.size()));
        if(lessonGroups.size() == 0){
            return;
        }
        // for each lesson group in the student's lesson group array list
        for (LessonGroup lessonGroup : lessonGroups){
            Log.d("BoughtPrice", String.valueOf(lessonGroup.getBoughtPrice()));
            Log.d("ID", lessonGroup.getLessonGroupId());
            Log.d("StudentID", lessonGroup.getStudentId());

            // get the corresponding courseID attribute in the lessonGroup object
            String courseID = lessonGroup.getCourseID();
            Log.d("CourseID", "CourseID is null: " + String.valueOf((courseID == null)));
}

当我将对象上传到 Firestore 时没有错误,但是当我检索数据时,我得到 null。 Log.d() 显示学生用户对象不为 null,LessonGroup 对象不为 null,LessonGroup 对象的其他属性也不为 null。只有 courseID 属性为 null,但它不应该为 null,因为在 Firestore 中它显然具有非空字符串值。为什么会出现这种情况?

在 Firestore 中,courseID 的值为“ec08d467-19d8-4f0f-a6ce-53303227a351”

检查此处所附的屏幕截图: Android Studio 日志显示 courseID 为空 Firestore 数据库 LessonGroup 对象文档

java android firebase google-cloud-firestore nullpointerexception
1个回答
0
投票

问题可能与您的

courseID
命名不符合 JavaBeans 标准有关。

尝试将课程 ID 访问器方法的名称更改为带有小写“d”的

getCourseId
。 您还应该将文档字段的名称更改为带有小写“d”的
courseId
。 这将匹配您也使用小写且正常工作的其他字段和访问器。

© www.soinside.com 2019 - 2024. All rights reserved.