[jboss-user] [EJB 3.0] - Re: Another NULL Pointer Exception on Entity Manager =/

Tchello do-not-reply at jboss.com
Thu Feb 12 12:29:50 EST 2009


 Ok, as you sad: i createad a new project respecting all those rules (i hope).
 Lets see:

 JSP file:

  | <%@ page language="java"  
  | isThreadSafe="false" 
  | import="javax.naming.*" 
  | import="javax.rmi.PortableRemoteObject"
  | import="org.interfaces.*"
  | import="org.classes.*"
  | %>
  | <html>
  |     <head>
  |         <title>JSP Page</title>
  |     </head>
  |     <body>
  |         <%
  |         ManagerRemote mn = null;
  |         InitialContext ic = null;
  |         
  |         try {
  |             out.print("Starting...<BR>");
  |             ic = new InitialContext();
  |          
  |             out.print("Initial instanced<BR>");
  |             mn = (ManagerRemote) ic.lookup("Manager/ManagerBean/remote");
  |             out.print("Manager obteined<BR>");
  |             
  |             out.print("Salvando...<BR>");
  |             mn.save();
  |             out.print("Cadastro salvo com sucesso!<BR><BR>");
  |             out.print("Pesquisando...<BR><BR>");
  |             
  |             out.print("<BR><BR>"+mn.getById(1)+"<BR><BR>");
  |             
  |             
  |         } catch(Exception e){
  |             out.println("ERRO <BR>");
  |             out.println(e.getMessage());
  |         }
  |         %> 
  |     </body>
  | </html>
  | 

ManagerBean:
 
  | 
  | package org.classes;
  | 
  | import javax.ejb.Stateful;
  | import javax.persistence.EntityManager;
  | import javax.persistence.PersistenceContext;
  | import org.interfaces.*;
  | 
  | @Stateful
  | public class ManagerBean implements ManagerLocal,ManagerRemote{
  |     
  |     //## Atributes
  |     @PersistenceContext(unitName="MyPostgresDS")
  |     private EntityManager em;
  | 
  |     //##Methods
  |     //##-----------------------------------------------------------------
  |     public ManagerBean() {
  |     }
  |     //##-----------------------------------------------------------------
  |     public void save(){
  |         Cadastro cd = new Cadastro();
  |         cd.setId(1);
  |         cd.setNome("Marcelo");
  |         
  |         em.getTransaction().begin();
  |         em.persist(cd);
  |         em.getTransaction().commit();
  |     }
  |     //##-----------------------------------------------------------------
  |     public String getById(int id){
  |         em.getTransaction().begin(); //is it really necessary?
  |         Cadastro found = em.find(Cadastro.class,id);
  |         em.getTransaction().commit(); //is it really necessary?
  |         
  |         return(found.getNome());
  |     }
  |     //##-----------------------------------------------------------------
  | }
  | 

Entity:

  | 
  | package org.classes;
  | 
  | import java.io.Serializable;
  | import javax.persistence.Column;
  | import javax.persistence.Entity;
  | import javax.persistence.Id;
  | import javax.persistence.Table;
  | 
  | 
  | @Entity
  | @Table(name="cadastro")
  | public class Cadastro implements Serializable{
  |     
  |     //## Atributes
  |     private Integer id;
  |     private String nome;
  |     
  |     //##Methods
  |     //##-----------------------------------------------------------------
  |     public Cadastro() {
  |     }
  |     //##-----------------------------------------------------------------
  |     @Id
  |     @Column(name="id")
  |     public Integer getId() {
  |         return id;
  |     }
  |     //##-----------------------------------------------------------------
  |     @Column(name="Nome")
  |     public String getNome() {
  |         return nome;
  |     }
  |     //##-----------------------------------------------------------------
  |     public void setNome(String nome) {
  |         this.nome = nome;
  |     }
  |     //##-----------------------------------------------------------------
  |     public void setId(Integer id) {
  |         this.id = id;
  |     }
  |     //##-----------------------------------------------------------------
  | }
  | 

persistence.xml

  | <?xml version="1.0" encoding="UTF-8"?>
  | 
  | <persistence>
  |     <persistence-unit name="myunit">
  |         <jta-data-source>java:MyPostgreDS</jta-data-source>
  |         <class>org.classes.Cadastro</class>
  |         <properties>
  |             <propertie name="org.hibernate.hdm2ddl" value="create-table">
  |         </properties>
  |     </persistence-unit>
  | </persistence>
  | 

Data Source: (also added to postgres-ds.xml on jboss deploy folder)

  | <datasources>
  |   <local-tx-datasource>
  |     <jndi-name>MyPostgreDS</jndi-name>
  |     <connection-url>jdbc:postgresql://192.168.0.200:5432/netejb</connection-url>
  |     <driver-class>org.postgresql.Driver</driver-class>
  |     <user-name>postgres</user-name>
  |     <password>postgres</password>
  |   </local-tx-datasource>
  | </datasources>
  | 

 Now i'm getting the following error:
anonymous wrote : 
  | Manager not bound
  | 
 It happens when i try to instance my ManagerBean (remote) in jsp file.

 Am i at the right way now?
 i apologize about my noob skills >.<
 Thank you!

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

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



More information about the jboss-user mailing list