在JSF

时间:2017-04-13 13:45:31

标签: jsf nullpointerexception ejb

我想在我的@ManagedBean课程中使用服务层,但我已经NullPointerException了。 这是我的界面

@Remote
public interface FieldService {

    void insertField(Field field);
    List<Field> getListOfFields();
    Field getFieldByTitle(String title);
    void removeField(Field field);

}

这是我对这个界面的实现

    @Stateless
public class FieldServiceImpl implements FieldService {
    private FieldDao fieldDao = new FieldDaoImpl();

    public void insertField(Field field) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            fieldDao.insertField(field, session);
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
        } finally {
            closeSession(session);
        }
    }

    public List<Field> getListOfFields() {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            return fieldDao.getListOfFields(session);
        } catch (Exception e) {
            return new ArrayList<Field>();
        } finally {
            closeSession(session);
        }
    }

    public Field getFieldByTitle(String title) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            return fieldDao.getFieldByTitle(title, session);
        } catch (Exception e) {
            return null;
        } finally {
            closeSession(session);
        }
    }

    public void removeField(Field field) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            fieldDao.removeField(field, session);
            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
        } finally {
            closeSession(session);
        }

    }

    private void closeSession(Session session) {
        if (Objects.nonNull(session)) {
            session.close();
        }
    }

}

这是我的FieldManageBean课程

@ManagedBean(name = "fieldManageBean")
@RequestScoped
public class FieldManageBean {
    private Field field;

    @EJB
    private FieldService fieldService;

    @PostConstruct
    public void init() {
        field = new Field();
    }

    public void addField() {
        System.out.println(field);
        fieldService.insertField(field);
    }

    public List<String> getFieldTypes() {
        return Type.getStringListOfEnums();
    }

    public Field getField() {
        return field;
    }

    public void setField(Field field) {
        this.field = field;
    }

}`

当我尝试将addField()函数调用到我的xhtml页面时,它会在NullPointerException fieldService.insertField(field);给我typedef struct { uint32_t power : 3; uint32_t reserved : 24; uint32_t speed : 5; } ctrl_t; void set_ctrl() { ctrl_t r = { .power = 1; .speed = 22; .reserved = 0; } uint32_t *addr = 0x12345678; *addr = *((uint32_t *) &r); return; } 问题是什么?

0 个答案:

没有答案