Java.util.List将对象添加到列表中不起作用

时间:2019-12-24 18:30:34

标签: java nullpointerexception

我想创建保留对象的列表,但我不知道为什么这不起作用。

private List<Reservation> reserfac;
private int resid;

public ServiceComptable() {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session =sf.openSession();
    Transaction tx =session.beginTransaction();

    try {
        List<Object[]> results = session.createQuery("select  R.total, R.nom , R.prenom, R.date_from,R.daynumber,R.npassport, F.id, F.date_creation "
                + " FROM  Reservation R join Facture F on R.Id=F.idReserv ").getResultList();

        for (Object[] result : results) {
            Reservation tmp = new Reservation();
            tmp.setTotal((double)result[0]);
            tmp.setNom((String )result[1]);
            tmp.setPrenom((String )result[2]);
            tmp.setDate_from((Date)result[3]);
            tmp.setDaynumber((int)result[4]);
            tmp.setNpassport((String)result[5]);
            tmp.setId((int)result[6]);
            tmp.setDate_to((Date)result[3]);
            reserfac.add((Reservation)tmp);
        }

        tx.commit();

    }catch (Exception e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        session.close();
    }

}
java.lang.NullPointerException
    at com.hotel.service.ServiceComptable.<init>(ServiceComptable.java:50)

编辑:使用“ new Arraylist <>();”初始化列表可解决此异常。

0 个答案:

没有答案