问题
I am trying to implement Search filter functionality and load grid in spring boot data JPA
application. For creating dynamic query I am using Querydsl
.
I am searching data according to sFloor and nBuildId.
If I am passing sFloor, nBuildId only that matching record should display in grid
If I am not passing any values then grid should load with all values.
I tried like below In that when I am passing data I am able to filter data. But when I am not passing any records I am getting null pointer exception
.
RoomController
@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);
}
RoomService
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();
}
Can any one please tell me why I am getting null pointer exception instead of all data?
回答1:
I think the issue is if (nBuildId != 0)
. nBuildId
is a big Integer
so when the check is being performed it is being unboxed to a primitive int
. If it's null, this will cause a NullPointerException
. A null check on nBuildId
should fix things, E.G. if (nBuildId != null && nBuildId != 0)
.
来源:https://stackoverflow.com/questions/52111072/spring-with-querydsl-null-pointer-exception