【问题标题】:nullpointerexception while using if condition使用 if 条件时出现空指针异常
【发布时间】:2015-04-01 17:19:57
【问题描述】:

我使用带有弹簧的休眠标准(两个注释)我有nullpointerexception 这是我的服务类:

    public List<Voiture> rechercher_voiture(String type, String couleur,
         String lieuLocation) {
    List<Voiture> v = new ArrayList<Voiture>();
    Criteria crit;
    Session session = dao.getSessionFactory().getCurrentSession();


    crit = session.createCriteria(Voiture.class)
            .add(Restrictions.eq("type", type))
            .add(Restrictions.eq("lieuLocation", lieuLocation))
            .add(Restrictions.eq("statut", (long) 0));


        if(couleur.isEmpty()==false) {
            crit.add(Restrictions.eq("couleur", couleur));
        }
v = crit.list();

    return v;
}

nullpointerexception 是由 if 条件引起的,有什么建议吗?

【问题讨论】:

  • 可能是因为 couleur == null ?
  • null 或不总是错误

标签: spring hibernate criteria


【解决方案1】:

如果couleur = null,就会发生这种情况。改成if(couleur!=null &amp;&amp; couleur.isEmpty()==false)

【讨论】:

    猜你喜欢
    • 2020-03-21
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多