Spring with Querydsl : Null Pointer Exception

淺唱寂寞╮ 提交于 2019-12-11 15:39:18

问题


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.

  1. If I am passing sFloor, nBuildId only that matching record should display in grid

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!