0

我的 PujaController.java:

public class PujaController implements Serializable {

private Puja current;
private DataModel items = null;
@EJB
private jpa.sessions.PujaFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;

public PujaController() {
}

public Puja getSelected() {
    if (current == null) {
        current = new Puja();
        current.setPujaPK(new jpa.entities.PujaPK());
        selectedItemIndex = -1;
    }
    return current;
}

private PujaFacade getFacade() {
    return ejbFacade;
}


public String creaPuja(Subasta subasta) {
    try {
        current = new Puja();
        current.setPujaPK(new jpa.entities.PujaPK());
        selectedItemIndex = -1;

        current.getPujaPK().setNroDePuja(0); //AUTO_INCREMENT
        Postor postor = AuthenticationPhaseListener.getLoggedInPostor();
        current.getPujaPK().setEmail(postor.getEmail());
        current.getPujaPK().setSubastaId(subasta.getSubastaId());

        Calendar cal = Calendar.getInstance();
        Date now = cal.getTime();
        current.setFechaDePuja(new java.sql.Timestamp(now.getTime()));
        current.setMontoAPagar(BigDecimal.ONE);
        current.setMontoDePuja(BigDecimal.TEN);
        current.setNroDePujas(0);
        current.setPagoAcumulado(BigDecimal.ONE);

        getFacade().create(current);
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("PujaCreated"));
        return "Puja";
    } catch (Exception e) {
        System.out.println(e);
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        return null;
    }
}


public String prepareCreate() {
    current = new Puja();
    current.setPujaPK(new jpa.entities.PujaPK());
    selectedItemIndex = -1;
    return "Create";
}

public String create() {
    try {
        current.getPujaPK().setNroDePuja(0); //AUTO_INCREMENT
        current.getPujaPK().setEmail(current.getPostor().getEmail());
        current.getPujaPK().setSubastaId(current.getSubasta().getSubastaId());
        getFacade().create(current);
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("PujaCreated"));
        return prepareCreate();
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        return null;
    }
}

}

我的 SubastaController.java:

public class SubastaController implements Serializable {

private Subasta current;
private DataModel items = null;
@EJB
private jpa.sessions.SubastaFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;

public SubastaController() {
}

public Subasta getSelected() {
    if (current == null) {
        current = new Subasta();
        selectedItemIndex = -1;
    }
    return current;
}

private SubastaFacade getFacade() {
    return ejbFacade;
}


public String preparePuja() {
    current = (Subasta) getItems().getRowData();
    selectedItemIndex = pagination.getPageFirstItem() +    getItems().getRowIndex();
    return "Puja";
}

public String pujar() {
    try {
        getFacade().edit(current);
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SubastaUpdated"));
        PujaController pc = new PujaController();
        pc.creaPuja(current);
        return "Puja";
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        return null;
    }
}

public String prepareCreate() {
    current = new Subasta();
    selectedItemIndex = -1;
    return "Create";
}

public String create() {
    try {
        getFacade().create(current);
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("SubastaCreated"));
        return prepareCreate();
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        return null;
    }
}

}

嗨,尝试学习一些 JSF,EntityManager API,我被困在这里:当 pujar() 执行时,subasta 成功更新,然后执行 pujaController 中的 creaPuja(current)。creaPuja(current) 直到 getFacade().create(current); 执行并给我一个 NullPointerException。创建当前对象并设置密钥。我找不到获取对实体管理器的引用以成功创建 puja 的方法。我还包括 Facade java 文件。如果你能提供一些代码来解释这个问题会很棒!!!

pujaFacade.java:

public class PujaFacade extends AbstractFacade<Puja> {
@PersistenceContext(unitName = "subasta701WPU")
private EntityManager em;

protected EntityManager getEntityManager() {
    return em;
}

public PujaFacade() {
    super(Puja.class);
}

}

abstractFacade.java:

public abstract class AbstractFacade<T> {
private Class<T> entityClass;

public AbstractFacade(Class<T> entityClass) {
    this.entityClass = entityClass;
}

protected abstract EntityManager getEntityManager();

public void create(T entity) {
    getEntityManager().persist(entity);
}

public void edit(T entity) {
    getEntityManager().merge(entity);
}

public void remove(T entity) {
    getEntityManager().remove(getEntityManager().merge(entity));
}

@PostConstruct
public T find(Object id) {
    return getEntityManager().find(entityClass, id);
}

@PostConstruct
public List<T> findAll() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return getEntityManager().createQuery(cq).getResultList();
}

public List<T> findRange(int[] range) {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    q.setMaxResults(range[1] - range[0]);
    q.setFirstResult(range[0]);
    return q.getResultList();
}

public int count() {
    javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
    javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
    cq.select(getEntityManager().getCriteriaBuilder().count(rt));
    javax.persistence.Query q = getEntityManager().createQuery(cq);
    return ((Long) q.getSingleResult()).intValue();
}

}

这是例外:

INFO: java.lang.NullPointerException
at jsf.PujaController.creaPuja(PujaController.java:146)
at jsf.SubastaController.pujar(SubastaController.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)

是的,getFacade() 为空。我的问题是如何将当前对象移动或分配给 getFacade()。有一个 PujaFacade 类型的成员 ejbFacade,但我需要 ejbFacade 获取当前(Puja 对象)的值,因此 getFacade 不会为空。谢谢 ...

4

1 回答 1

0

抛出异常时getFacade()current的值是多少。

另外,您能否发布异常中的堆栈跟踪。

注意:根据我的经验,NullPointerException 是从空对象访问方法的结果。在这种情况下,这意味着 getFacade() 返回 null。

更新: 查看您的代码后,我不确定。您从未在 PujaController 或 SubastaController 中设置 ejbFacade,但可以在 SubastaController 中访问它,但不能在 PujaController 中访问它。尝试将 jpa.sessions.SubastaFacade 实现与 jpa.sessions.PujaFacade 实现进行比较,看看是否遗漏了什么。

例如,PujaFacade 在类定义之前是否需要@Stateless?@PersistenceContext(unitName = "subasta701WPU") 中的单位名称是否应该更改?

您可能还想阅读以下内容:

Java EE 6 应用程序入门

Java Persistence API - 一种更简单的实体持久性编程模型

一个维基百科页面,里面有一些很好的样本

让我们知道结果如何。

于 2012-02-11T01:56:11.227 回答