[英]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.