2013-01-18 66 views
0

我得到了空指針異常,我不知道爲什麼。在DB我有這樣一個表(lekarz指醫生在波蘭):空指針異常,EJB,JPA webapp

create table Lekarz(
idlekarz int unique not null primary key, 
poziom int not null references Poziom_dostepu(idpoziom), 
specjalnosc varchar(50) not null); 

這裏是實體類:

@Entity 
@Table(name = "lekarz") 
@DiscriminatorValue("lekarz") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "Lekarz.findAll", query = "SELECT l FROM Lekarz l"), 
    @NamedQuery(name = "Lekarz.findById", query = "SELECT l FROM Lekarz l WHERE l.idpoziom = :idpoziom"), 
    @NamedQuery(name = "Lekarz.findBySpecjalnosc", query = "SELECT l FROM Lekarz l WHERE l.specjalnosc = :specjalnosc")}) 
public class Lekarz extends PoziomDostepu implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Basic(optional = false) 
    @NotNull 
    @Size(min = 1, max = 50) 
    @Column(name = "specjalnosc") 
    private String specjalnosc; 
    @JoinColumn(name = "idpoziom", referencedColumnName = "idpoziom") 
    @ManyToOne(optional = false) 
    private PoziomDostepu poziom; 
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "lekarz") 
    private Collection<Pacjent> pacjentCollection; 
    @JoinColumn(name = "osoba", referencedColumnName = "idosoba") 
    @OneToOne(optional = false) 
    private Osoba osoba; 

    public Lekarz() { 
    } 

    public Osoba getOsoba() { 
     return osoba; 
    } 

    public void setOsoba(Osoba osoba) { 
     this.osoba = osoba; 
    } 

    public String getSpecjalnosc() { 
     return specjalnosc; 
    } 

    public void setSpecjalnosc(String specjalnosc) { 
     this.specjalnosc = specjalnosc; 
    } 

    public PoziomDostepu getPoziom() { 
     return poziom; 
    } 

    public void setPoziom(PoziomDostepu poziom) { 
     this.poziom = poziom; 
    } 

    @XmlTransient 
    public Collection<Pacjent> getPacjentCollection() { 
     return pacjentCollection; 
    } 

    public void setPacjentCollection(Collection<Pacjent> pacjentCollection) { 
     this.pacjentCollection = pacjentCollection; 
    } 

    @Override 
    public String toString() { 
     return "pl.karta.epacjent.entities.Lekarz[ idlekarz=" + idpoziom + " ]"; 
    } 

} 

門面:

@Stateless 
public class LekarzFacade extends AbstractFacade<Lekarz> implements LekarzFacadeLocal { 
    @PersistenceContext(unitName = "epacjentPU") 
    private EntityManager em; 

    @Override 
    protected EntityManager getEntityManager() { 
     return em; 
    } 

    public LekarzFacade() { 
     super(Lekarz.class); 
    } 



} 

當地門面:

@Local 
public interface LekarzFacadeLocal { 
    void create(Lekarz Lekarz); 
    void edit(Lekarz konto); 
    void remove(Lekarz konto); 
    Lekarz find(Object id); 
    List<Lekarz> findAll(); 
    List<Lekarz> findRange(int[] range); 
    int count(); 
} 

函數findAll()in abstract f acade:

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

而我只是有這樣的函數中的bean:

public List<Lekarz> getListaLekarzy() 
    { 
      return lekarzfacade.findAll(); 
    } 

NULL指針,因爲這個功能(getListaLekarzy),但我敢肯定有表Lekarz 1項紀錄DB,所以我應該得到這個記錄的1元素列表。我對嗎?

在此先感謝您的幫助。

EDIT 1: 門面lekarzBean:

@Stateless 
@LocalBean 
@ManagedBean(name = "LekarzBean") 
@SessionScoped 
public class LekarzBean { 

    @EJB 
    private EndpointLocal endpoint; 
    private LekarzFacadeLocal lekarzfacade; 

堆棧跟蹤

javax.faces.el.EvaluationException: java.lang.NullPointerException 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) 
    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:1542) 
    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 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) 
    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:722) 
Caused by: java.lang.NullPointerException 
    at pl.karta.epacjent.beans.ListaPacjentowBean.getListaLekarzy(ListaPacjentowBean.java:52) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779) 
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:528) 
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:248) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
    ... 32 more 
+0

空指針意味着對象爲空。調試您的應用程序,以便了解哪個對象爲空,或發佈堆棧跟蹤。你如何在bean中創建/注入'lekarzfacade'? – perissf

+0

'at pl.karta.epacjent.beans.ListaPacjentowBean.getListaLekarzy(ListaPacjentowBean.java:52)'ListaPacjentowBean實際上是LekarzBean。 –

回答

2

@EJB添加註解來private LekarzFacadeLocal lekarzfacade;

@EJB 
private EndpointLocal endpoint; 
@EJB 
private LekarzFacadeLocal lekarzfacade; 
+0

它的工作原理!謝謝:)我現在應該看看爲什麼這個註釋在這種情況下非常重要... –