0

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
  • On which line do you get the exception? Commented Aug 31, 2018 at 8:41

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

its working properly Thank you. one more thing is I want particular column from Query Result. so How I can iterate this List<Tuple>
I tried by using for each loop but getting class cast exception ` List<Tuple> queryResult = query.fetch(); for(Tuple row : queryResult) { System.out.println("Build Id " +row.get(room.nBuildId)); }
Raise a separate question as this is unrelated to the NullPointerException.
Ok. https://stackoverflow.com/questions/52114040/spring-with-querydsl-java-lang-classcastexception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.