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

NullPointerException when using EntityManager and Criteria in Java

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

当我单独发送没有日期的标题时,系统工作正常; 当我发送带有日期的标题时,系统工作正常,正确返回 objects 的列表; 当我发送没有标题的日期时,系统抛出 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");
    }


}

我没有发布其他 class 是因为我已经调试并且这个 class 的属性很好。问题是当我不发送字段 "title" 时,这意味着当它为 null(不需要)时,查询不起作用

很可能 NullPointerException 的来源在这里:

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

请将“&”替换为“&&”并查看结果。