Google App Engine 中尝试获取实体中的字段值时出现 NullPointerException 异常
NullPointerException Exception when trying to get the value of a field in an Entity in Google App Engine
我有一个学生实体,其中有一个字段是对这样的部门实体的引用。
@Load private @Index Ref<Department> department;
然后 Department 字段的 getter 和 setter 如下所示
public Department getDepartment() {
return department.get();
}
public void setDepartment(Key<Department> department) {
this.department = Ref.create(department);
}
学生实体中还有一个字段,其中包含对 SchoolFaculty 实体的引用
@Load
private @Index Ref<SchoolFaculty> schoolFaculty;
有以下getter和setter
public SchoolFaculty getSchoolFaculty() {
if (schoolFaculty != null)
return schoolFaculty.get();
return null;
}
public void setSchoolFaculty(Key<SchoolFaculty> schoolFaculty) {
this.schoolFaculty = Ref.create(schoolFaculty);
}
Department 实体中有一个字段,其中包含 Faculty 实体的键,如下所示
@Index
private Key<SchoolFaculty> schoolFaculty;
字段的 getter 和 setter 如下所示
public String getSchoolFaculty() {
return schoolFaculty.toWebSafeString();
}
public void setSchoolFaculty(Key<SchoolFaculty> schoolFaculty) {
this.schoolFaculty = schoolFaculty;
}
我使用 google Api Explorer 在本地数据存储中创建了部门和 SchoolFaculty 实体,部门将其 SchoolFaculty 引用设置为有效值,该值也存在于 SchoolFaculty table,那是该部门有教员,但是当我尝试使用以下代码设置学生的 SchoolFaculty 时,我得到了 NullPointerException。这是代码
student.setDepartment(Key.create(Department.class,student_programme.getDepartmentRef()));// student_programme is a reference to an Entity which I use to get the department and it works fine and the student gets its Department set to the Department Value.
if(student.getDepartment().getSchoolFaculty()) != null){ student.setSchoolFaculty(Key.create(SchoolFaculty.class,getDepartment().getSchoolFaculty()));}// this is the line throws a null pointer Exception
我正在为数据存储事务使用 objectify,并且已添加对 objectify 依赖项的所有引用。
因为 student.getDepartment().getSchoolFaculty()
在触发条件语句之前抛出空指针,并且 google appengine 键无法为空实体或 websafestring 创建键。因此,请仔细调试并检查 getDepartment()
是否首先返回 null 或它 returns 的内容,如果要使用 Key 创建带有数据存储的键的空字段,则使用 默认值 例如
Key.create(SchoolFaculty.class, "default"); // when it is null
希望这有用
我有一个学生实体,其中有一个字段是对这样的部门实体的引用。
@Load private @Index Ref<Department> department;
然后 Department 字段的 getter 和 setter 如下所示
public Department getDepartment() {
return department.get();
}
public void setDepartment(Key<Department> department) {
this.department = Ref.create(department);
}
学生实体中还有一个字段,其中包含对 SchoolFaculty 实体的引用
@Load
private @Index Ref<SchoolFaculty> schoolFaculty;
有以下getter和setter
public SchoolFaculty getSchoolFaculty() {
if (schoolFaculty != null)
return schoolFaculty.get();
return null;
}
public void setSchoolFaculty(Key<SchoolFaculty> schoolFaculty) {
this.schoolFaculty = Ref.create(schoolFaculty);
}
Department 实体中有一个字段,其中包含 Faculty 实体的键,如下所示
@Index
private Key<SchoolFaculty> schoolFaculty;
字段的 getter 和 setter 如下所示
public String getSchoolFaculty() {
return schoolFaculty.toWebSafeString();
}
public void setSchoolFaculty(Key<SchoolFaculty> schoolFaculty) {
this.schoolFaculty = schoolFaculty;
}
我使用 google Api Explorer 在本地数据存储中创建了部门和 SchoolFaculty 实体,部门将其 SchoolFaculty 引用设置为有效值,该值也存在于 SchoolFaculty table,那是该部门有教员,但是当我尝试使用以下代码设置学生的 SchoolFaculty 时,我得到了 NullPointerException。这是代码
student.setDepartment(Key.create(Department.class,student_programme.getDepartmentRef()));// student_programme is a reference to an Entity which I use to get the department and it works fine and the student gets its Department set to the Department Value.
if(student.getDepartment().getSchoolFaculty()) != null){ student.setSchoolFaculty(Key.create(SchoolFaculty.class,getDepartment().getSchoolFaculty()));}// this is the line throws a null pointer Exception
我正在为数据存储事务使用 objectify,并且已添加对 objectify 依赖项的所有引用。
因为 student.getDepartment().getSchoolFaculty()
在触发条件语句之前抛出空指针,并且 google appengine 键无法为空实体或 websafestring 创建键。因此,请仔细调试并检查 getDepartment()
是否首先返回 null 或它 returns 的内容,如果要使用 Key 创建带有数据存储的键的空字段,则使用 默认值 例如
Key.create(SchoolFaculty.class, "default"); // when it is null
希望这有用