[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - NPE with EntityManagerFactory

Hellek do-not-reply at jboss.com
Wed May 16 21:56:20 EDT 2007


I always get a NullPointerException with this:

public class Servlet1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
  | 	 
  | 	 @PersistenceUnit(name="EJBProject1")
  | 	 EntityManagerFactory emf;	
  | 
  | 	 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  | 	 response.setContentType("text/html"); 
  | 	 response.setHeader("Pragma", "No-cache");
  | 	 response.setDateHeader("Expires", 0);
  | 	 response.setHeader("Cache-Control", "no-cache");
  | 
  | 	 EntityManager em1 = emf.createEntityManager();
  | 	 Query query1 = em1.createQuery("select b from benutzer b");
  | 	 List users = query1.getResultList();
  | 	 PrintWriter writer = response.getWriter();
  | 
  | 	 for (Iterator iterator = users.iterator(); iterator.hasNext();) {
  | 	 Benutzer benutzer = (Benutzer) iterator.next();
  | 	 writer.print(benutzer.getName());
  | 	 }
  | 	 writer.flush();
  | 	 } 
  | }

I failed to figure out whats going wrong for hours, had my problems with some other issues too. But unlike those, I did not manage to fix this one.

The exception:

03:29:09,125 ERROR [[Servlet1]] Servlet.service() for servlet Servlet1 threw exception
  | java.lang.NullPointerException
  | 	at com.Servlet1.doGet(Servlet1.java:46)
  | 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
  | 	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
  | 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
  | 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  | 	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
  | 	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
  | 	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  | 	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
  | 	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  | 	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
  | 	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
  | 	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
  | 	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
  | 	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
  | 	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  | 	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
  | 	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
  | 	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
  | 	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
  | 	at java.lang.Thread.run(Unknown Source)
  | 

I use JBoss 4.2.0 and Eclipse 3.3M6 with M6 WTP.

Here is the persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
  | <persistence version="1.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_1_0.xsd">
  | 	<persistence-unit name="EJBProject1">
  | 		<provider>org.hibernate.ejb.HibernatePersistence</provider>
  |     	<jta-data-source>java:TestDS</jta-data-source>
  | 		<class>com.Benutzer</class>
  | 		<properties>
  | 		  <property name="hibernate.dialect"
  | 				value="org.hibernate.dialect.MySQLDialect" />
  | 		  <property name="hibernate.transaction.manager_lookup_class" 
  |             value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
  | 		  
  | 		</properties>
  | 	</persistence-unit>
  | </persistence>
  | 

The Datastore-Definition (I put the driver in place and it gets loaded successfully according to the logs)

<local-tx-datasource>
  |     <jndi-name>TestDS</jndi-name>
  |     <connection-url>jdbc:mysql://...:3306/test</connection-url>
  |     <driver-class>com.mysql.jdbc.Driver</driver-class>
  |     <user-name>...</user-name>
  |     <password>...</password>    
  |     <min-pool-size>5</min-pool-size>
  |     <max-pool-size>20</max-pool-size>    
  | 
  |       <metadata>
  |        <type-mapping>mySQL</type-mapping>
  |     </metadata>  
  |     </local-tx-datasource>

By the way, I came across this problem while following this tutorial: http://www.eclipse.org/webtools/jst/components/j2ee/scenarios/jee5_application_creation_tutorial.html
I want to do what is done there, with JBoss, Hibernate and a MySQL Server

Here you can find my project files: http://hellekalek.com/diverses/workspace.zip

I really hope somebody can tell me what I am doing wrong. I lost all hope. Its just such a small tutorial that I want to follow, it can't be that difficult ... I thought. (of course its a little different because they don't use JBoss, but its really not a big project)

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4046344#4046344

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4046344



More information about the jboss-user mailing list