在Java中使用EntityManager和Criteria时出现NullPointerException



我创建了一个类,它根据前端应用程序中填充的一些过滤器进行查询。基本上有3个过滤器:"Title"、"startDate"one_answers"endDate"。以下是我面临的问题:

当我单独发送没有日期的标题时,系统工作正常;当我发送带有日期的标题时,系统工作正常,正确返回对象列表;当我发送不带标题的日期时,系统会抛出一个NullPointerException。

public class RacRepositoryImpl implements RacRepositoryQuery  {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<Rac> filtrar(RacFilter racFilter) throws Exception {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Rac> query = builder.createQuery(Rac.class);
Root<Rac> from = query.from(Rac.class);
Predicate predicate = builder.and();
//title
if (racFilter.getTitulo() != null & racFilter.getTitulo().length() > 2) {
predicate = builder.and(predicate, 
builder.like(from.<String>get("titulo"), "%"+racFilter.getTitulo()+"%"));
}
//startDate
if (racFilter.getDataInicial() != null) {
predicate = builder.and(predicate,
builder.greaterThanOrEqualTo(from.<Date>get("dataHora"), racFilter.getDataInicial()));
}
//finalDate
if (racFilter.getDataFinal() != null) {
predicate = builder.and(predicate,
builder.lessThanOrEqualTo(from.<Date>get("dataHora"), racFilter.getDataFinal()));
}
// here it throws the NullPointer exception when I don't send the title information 
TypedQuery<Rac> typedQuery = entityManager.createQuery(
query.select(from )
.where( predicate )
);
try {
List<Rac> results = typedQuery.getResultList();
return results;
} catch (Exception e) {
throw new Exception("not found");
}

}

我不会发布其他类,因为我已经调试过了,而且这个类的属性也很好。问题是,当我不发送字段"title"时,也就是说,当它为null时(这不是必需的(,查询就不能工作

NullPointerException的来源很可能在这里:

//title
if (racFilter.getTitulo() != null & racFilter.getTitulo().length() > 2) {

请用"&&"替换"&"并查看结果。

相关内容

  • 没有找到相关文章

最新更新