我正在尝试在spring boot data JPA
应用程序中实现搜索过滤器功能和加载网格。为了创建动态查询,我使用的是Querydsl
。
我正在根据sFloor和nBuildId搜索数据。
我尝试下面的内容当我传递数据时,我能够过滤数据。但是,当我没有通过任何记录时,我得到了null pointer exception
。
房间控制器
@GetMapping("/getUnclaimedRoomDetails")
public List<Tuple> populateUnclaimedRoomGridView(@RequestParam(value="nBuildId", required=false) Integer nBuildId,
@RequestParam(value="sFloor", required=false) String sFloor) {
return roomService.loadUnclamiedRoomGrid(nBuildId,sFloor);
}
客房服务
public List<Tuple> loadUnclamiedRoomGrid(Integer nBuildId, String sFloor) {
QRoom room = QRoom.room;
QRoomDepartmentMapping roomDepartmentMapping = QRoomDepartmentMapping.roomDepartmentMapping;
JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
query.from(room)
.where(room.nRoomId.notIn
(JPAExpressions.select(roomDepartmentMapping.nRoomId)
.from(roomDepartmentMapping)
)
);
if (nBuildId != 0) {
query.where(room.nBuildId.eq(nBuildId));
}
if(sFloor != null) {
query.where(room.sFloor.eq(sFloor));
}
return query.fetch();
}
任何人都可以告诉我为什么我得到空指针异常而不是所有数据?
我认为问题是if (nBuildId != 0)
。 nBuildId
是一个很大的Integer
所以当检查正在执行时,它正被拆箱到原始的int
。如果它为null,这将导致NullPointerException
。对nBuildId
的空检查应该解决问题,E.G。 if (nBuildId != null && nBuildId != 0)
。