Search code examples
javamavenspring-mvcspring-data-jpaquerydsl

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. That's why I opted for queryDsl . This is my pom.xml

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

I made 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

 @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. Any help please. It's been hours that i'm blocked


Solution

  • I expect "send an empty field" means that in your controller the parameter is null. Therefore accessing it's methods will throw an exception. Add a null check in the controller and you should be good.