私は試験の目的で小さなWebアプリケーションを開発していますが、Daoメソッドでいくつかの問題が発生しています。それらはすべてNullPointerExceptionを返します。 Administrator.javaDAOメソッドはすべてNullPointerExceptionを返します
public class AdministratorDaoJPA implements AdministratorDao {
@PersistenceContext(unitName = "clinic-unit", type = PersistenceContextType.EXTENDED)
private EntityManager em;
@Override
public Administrator findByUsername(String username){
Query query = em.createQuery("SELECT a FROM Administrator a WHERE a.username=?");
return (Administrator)query.setParameter(1,username).getSingleResult();
}
そして、そこです:
@Entity
@NamedQuery(name="allAdministrators", query="SELECT a FROM Administrator a")
public class Administrator {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true, nullable = false)
private String username;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String surname;
@Column(nullable = false)
private String pwd;
public Administrator(){}
public Administrator(String username, String name, String surname, String pwd) {
this.username=username;
this.name = name;
this.surname=surname;
this.pwd = pwd;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username ;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSurname() {
return surname;
}
public void setSurname(String surName) {
this.surname = surName;
}
@Override
public boolean equals(Object o) {
Administrator adm = (Administrator) o;
return adm.getUsername().equals(this.getUsername());
}
@Override
public int hashCode() {
return this.getUsername().hashCode();
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("Adinistrator");
sb.append("{id='").append(id);
sb.append(", name='").append(name);
sb.append(", surname='").append(surname);
sb.append(", username='").append(username);
sb.append("}\n");
return sb.toString();
}
}
そして最後にpersistence.xml:これはDAOで
@Stateless(name = "administratorFacade")
public class AdministratorFacade {
@PersistenceContext(unitName = "clinic-unit")
private EntityManager em;
public Administrator getAdministrator(String username){
AdministratorDaoJPA administratorDao = new AdministratorDaoJPA();
Administrator administrator = administratorDao.findByUsername(username);
return administrator;
} //An example of method that uses DAO
:
この
はファサードです<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="clinic-unit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>PostgreSQL Database</jta-data-source>
<class>it.clinic.model.Indicator</class>
<class>it.clinic.model.Administrator</class>
<class>it.clinic.model.Exam</class>
<class>it.clinic.model.ExamTypology</class>
<class>it.clinic.model.Medic</class>
<class>it.clinic.model.Patient</class>
<class>it.clinic.model.Prerequisite</class>
<properties>
<!-- ad ogni esecuzione viene creato lo schema, cancellando il contenuto delle tabelle
<property name="hibernate.hbm2ddl.auto" value="create"/> -->
<!-- SQL dialect -->
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<!--- Per avere info sulle istruzioni SQL inviate al db -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>
スタックトレース:
java.lang.NullPointerExceptionがit.clinic.persistence.ExamTypologyDaoJPA.findAll(ExamTypologyDaoJPA.java:54) it.clinic.facade.AdministratorFacade.getAllExamTypologies(AdministratorFacade.java:53) sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブメソッド) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Methodオブジェクト。 invoke(Method.java:498) org.apache.openejb.core.interceptor.ReflectionInvocationContext $呼び出し.invoke(ReflectionInvocationContext.java:205) org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186) org.apache.openejb.monitoring.StatsInterceptor.record(StatsInterceptor.java:181) org.apache.openejb.monitoring.StatsInterceptor.invoke(StatsInterceptor.java:100) sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブメソッド) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect。 Invoke.invoke(ReflectionInvocationContext.java:205)このメソッドは、 org.apache.openejb.core.interceptor.ReflectionInvocationContext.proceed(ReflectionInvocationContext.java:186) org.apache.openejb.core.interceptor.InterceptorStack.invoke(InterceptorStack.java:85) org.apache.openejb.core .stateless.StatelessContainer._invoke(StatelessContainer.java:236) org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:203) org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler .java:265) org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:260) org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:89) ORG。 apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:347) it.clinic.facade.AdministratorFacade $$ LocalBeanProxy.getAllExamTypologies(IT /クリニック/ファサード/ AdministratorFacade.java) it.clinic.controller.ExamTypologyController.examTypologiesList(ExamTypologyController.java:35) sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブメソッド) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) orgorg。(TomEEInjectionProvider.java:57) com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder。org.apache.tomee.mojarra.TomEInjectionProvider.invokePostConstruct(TomEEInjectionProvider.java:57) java:221) com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103) com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) com.sun.faces.mgbean .BeanManager.create(BeanManager.java:269) com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257) com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117) com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) com.sun.faces.el.ChainTypeCompositeELResolver.getValue(ChainTypeCompositeELResolver.java:90) org.apache.jasper.el.JasperELResolver.getValue (AstRedentifier.java:96) () ForEachSupport.java:151) LeopTagSupport.doStartTag(LoopTagSupport.java:256) org.apache.jsp.ExamTypologies_jsp._jspx_meth_c_005fforEach_005f0(ExamTypologies_jsp.java:280) org.apache.jsp.ExamTypologies_jsp._jspService(ExamTypologies_jsp.java:232) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java: 70) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) org.apache.jasper.servlet.JspServlet.serviceJspFile (JspServlet.java:396) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) javax.servlet.http.HttpServlet.service(HttpServlet.java:729) org.apache.tomcat。 websocket.server.WsFilter.doFilter(WsFilter.java:52) com.sun.faces.context.ExternalContextImpl.dispatch(Exte rnalContextImpl.java:642) com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:363) com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:153) コム.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:99) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl .java:219) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) org.apache.openejb。 (WebBeansFilter.java:52)
私はTomeeとJSF 2.2でJPAを使用しています。 は、私はあなたがこれを通過するために私を助けることを願っています:)
NPEスタックトレースを追加して、おそらくNPEの先頭に関連するソースの行を特定する必要があります。 – LhasaDad
ヘルプにthxを追加 –