我有空指针异常,我不知道为什么。在 DB 中,我有一张这样的表格(lekarz 在波兰语中是医生的意思):
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():
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();
}
空指针是因为这个函数(getListaLekarzy),但我确定 DB 中的 Lekarz 表中有 1 条记录,所以我应该得到这个记录的 1 元素列表。我对吗?
提前感谢所有帮助。
编辑 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