简体   繁体   English

使用SpringData的queryDsl谓词时出现空指针异常

[英]Null pointer exception when I use queryDsl Predicate of SpringData

Hi i'm trying to make a search using 4 fields but some of them might be null when I make the search. 嗨,我正在尝试使用4个字段进行搜索,但是当我进行搜索时,其中某些字段可能为null。 That's why I opted for queryDsl . 这就是为什么我选择使用queryDsl的原因。 This is my pom.xml 这是我的pom.xml

<dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
        </dependency>

I made ClientPredicate.java 我做了ClientPredicate.java

final class ClientPredicates {

    private ClientPredicates() {
    }

    static Predicate firstnameOrLastnameOrCinOrPhoneNberContainsIgnoreCase(String searchTerm) {
        if (searchTerm == null || searchTerm.isEmpty()) {
            return (Predicate) QClient.client.isNotNull();
        } else {
            return (Predicate) QClient.client.firstname.containsIgnoreCase(searchTerm)
                    .or(QClient.client.lastname.containsIgnoreCase(searchTerm)).or
                    (QClient.client.cin.containsIgnoreCase(searchTerm)).or(QClient.client.phoneNber.containsIgnoreCase(searchTerm));
        }
    }

    }

QClient.java QClient.java

 @Generated("com.mysema.query.codegen.EntitySerializer")
    public class QClient extends EntityPathBase<Client> {

        private static final long serialVersionUID = -797939782L;

        public static final QClient client = new QClient("client");

        public final StringPath createdByUser = createString("createdByUser");

        public final DateTimePath<java.time.ZonedDateTime> creationTime = createDateTime("creationTime", java.time.ZonedDateTime.class);

        public final StringPath firstname = createString("firstname");
        public final StringPath lastname = createString("lastname");
        public final StringPath cin = createString("cin");
        public final StringPath phoneNber = createString("phoneNber");

        public final NumberPath<Long> id = createNumber("id", Long.class);

        public final DateTimePath<java.time.ZonedDateTime> modificationTime = createDateTime("modificationTime", java.time.ZonedDateTime.class);

        public final StringPath modifiedByUser = createString("modifiedByUser");



        public final NumberPath<Long> version = createNumber("version", Long.class);

        public QClient(String variable) {
            super(Client.class, forVariable(variable));
        }

        public QClient(Path<Client> path) {
            super(path.getType(), path.getMetadata());
        }

        public QClient(PathMetadata<?> metadata) {
            super(Client.class, metadata);
        }

    }

And in my controller I have this code: 在我的控制器中,我有以下代码:

@RequestMapping(value = "/seekClient", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody List<Client> seekClient(@RequestBody Client client) {

        Predicate firstnameAndLastnameAndCinAndPhoneNberAre = QClient.client.firstname.eq(client.getFirstname())
                .and(QClient.client.lastname.eq(client.getLastname()));

        System.out.println("*************************** "+firstnameAndLastnameAndCinAndPhoneNberAre);
        List<Client> list = (List<Client>) clientRepository.findAll(firstnameAndLastnameAndCinAndPhoneNberAre);

        return list;

    }

The problem is every time I send a empty field I'm getting a nullPointerException. 问题是每次我发送一个空字段时,我都会得到一个nullPointerException。 Any help please. 请帮忙。 It's been hours that i'm blocked 我已经被封锁了几个小时

I expect "send an empty field" means that in your controller the parameter is null. 我希望“发送一个空字段”意味着在您的控制器中该参数为null。 Therefore accessing it's methods will throw an exception. 因此,访问它的方法将引发异常。 Add a null check in the controller and you should be good. 在控制器中添加一个空检查,您应该会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我使用 @InjectMocks 时,null 指针异常测试失败 - Test fails on a null pointer exception when I use @InjectMocks 当我尝试使用getActionCommand时,为什么会出现空指针异常 - why is there a null pointer exception when i try to use getActionCommand 尝试使用CommandExecutor时出现空指针异常 - Null Pointer Exception when trying to use a CommandExecutor 将 QueryDSl 与 Spring Boot App 集成时出现空指针异常 - Null pointer exception while integrating QueryDSl with Spring Boot App 单击setOnTouchListener时出现空指针异常 - Null pointer exception when i click on setOnTouchListener Android Volley - 在String上调用.length()时抛出空指针异常,但我不在任何地方使用.length() - Android Volley - Throwing a Null Pointer Exception when calling .length() on a String, but I don't use .length() anywhere Spring Data REST,QueryDSL和HashMaps - 在引用HashMap属性时从URL中为Null谓词 - Spring Data REST, QueryDSL and HashMaps - Null Predicate from URL when referring to HashMap properties QueryDSL多参数搜索,同时在值为null时构建查询跳过谓词 - QueryDSL multiparameter search, while building the query skip predicate when value is null 尝试将JNDI用于JDBC时出现空指针异常 - Null pointer exception when attempting to use JNDI for JDBC 尝试使用其他类中的方法时出现空指针异常 - Null Pointer Exception when attempting to use methods from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM