Spring with Querydsl:空指针异常 [英] Spring with Querydsl : Null Pointer Exception

查看:72
本文介绍了Spring with Querydsl:空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 spring boot data JPA 应用程序中实现搜索过滤器功能和加载网格.为了创建动态查询,我使用 Querydsl.

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.

我正在根据 sFloor 和 nBuildId 搜索数据.

I am searching data according to sFloor and nBuildId.

  1. 如果我传递 sFloor,nBuildId 只应在网格中显示匹配的记录

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

房间控制器

@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();   

}

谁能告诉我为什么我得到的是空指针异常而不是所有数据?

Can any one please tell me why I am getting null pointer exception instead of all data?

推荐答案

我认为问题在于 if (nBuildId != 0).nBuildId 是一个很大的 Integer,因此在执行检查时,它会被拆箱为原始 int.如果它为空,这将导致 NullPointerException.对 nBuildId 的空检查应该可以解决问题,例如if (nBuildId != null && nBuildId != 0).

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

这篇关于Spring with Querydsl:空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆