0

I want to use service layer in my @ManagedBean class but i've got NullPointerException. Here is my interface

@Remote
public interface FieldService {

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

}

Here is my realization of this interface

    @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();
        }
    }

}

And here is my FieldManageBean class`

@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;
    }

}`

When I try to invoke addField() function into my xhtml page, it gives me NullPointerException here fieldService.insertField(field); What's the problem?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Serg Shapoval
  • 707
  • 1
  • 11
  • 39
  • So this app doesn't run on a normal Java EE server but on a barebones servletcontainer like Tomcat? – BalusC Apr 13 '17 at 13:51
  • @BalusC it runs on tomcat – Serg Shapoval Apr 13 '17 at 13:54
  • Tomcat isn't a Java EE server. – BalusC Apr 13 '17 at 13:56
  • whether session created in your insertField Method? if not Check your HibernateUtil class. – Porkko M Apr 13 '17 at 13:57
  • I don't think it is a duplicate. However, if you have a remote business interface I suppose you are not packacing the ejb into the war file. In this case you cannot just mark as `@EJB` and expect it to work. You have to put in place all the mechanism to lookup, either via JNDI or trough annotation. It is independent of Tomcat because you are supposed to ask and obtain a proxy of the Ejb. – Leonardo Apr 13 '17 at 15:13

0 Answers0