users@glassfish.java.net

java.lang.NullPointerException when running client application

From: <glassfish_at_javadesktop.org>
Date: Mon, 18 May 2009 11:54:21 PDT

Hi,
I'm trying to develop a small application that consists of an entity bean, a session bean and a client. The application is supposed to add the book into the database using persistance and session bean. I'm using glassfish v2, netbeans and mysql.

I got stuck on one error:
[#|2009-05-18T18:49:53.093+0000|INFO|sun-appserver9.1|javax.enterprise.system.container.ejb|_ThreadID=33;_ThreadName=p: thread-pool-1; w: 43;|
javax.ejb.EJBException
        at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:3869)
        at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:3769)
        at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:3571)
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1354)
        at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1316)
        at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:210)
        at com.sun.ejb.containers.EJBObjectInvocationHandlerDelegate.invoke(EJBObjectInvocationHandlerDelegate.java:117)
        at $Proxy99.addBook(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:154)
        at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:687)
        at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:227)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1846)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1706)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:1088)
        at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:223)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:806)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:563)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2567)
        at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:555)
Caused by: java.lang.NullPointerException
        at beans.NewSessionBean.addBook(NewSessionBean.java:27)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1067)
        at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:176)
        at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2895)
        at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:3986)
        at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:203)
        ... 17 more
|#]


The complete code:
The client:
package javaapplication2;

//import javax.ejb.EJB;
import javax.naming.InitialContext;
import beans.*;
import javax.ejb.EJB;
import javax.naming.NamingException;

/**
 *
 * @author Nupek
 */
public class Main {
  @EJB
   private static NewSessionRemote tDDD05SessionBean;
    
        

    public static void main(String[] args) throws java.rmi.RemoteException, NamingException{
        // TODO code application logic here
        
                InitialContext ctx = new InitialContext();
                NewSessionRemote bean = (NewSessionRemote) ctx.lookup("ejb/TDDD05SessionBeanJNDI");
                try{
                
                        bean.addBook("122", "title1", "author");
                   
                                        }
                catch(Exception e){
                        e.printStackTrace();
                }
    }

}

session bean:
package beans;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import entity.Book;
import javax.ejb.Remote;

@Remote(NewSessionRemote.class)
@Stateless(name="TDDD05SessionBean", mappedName="ejb/TDDD05SessionBeanJNDI")
public class NewSessionBean implements NewSessionRemote {

@PersistenceContext(unitName="EJBPU")
private EntityManager em;
        
        public Book lib;

        public void addBook(String isbn, String title, String author) {
        
                lib.setIsbn(isbn);
                lib.setTitle(title);
                lib.setAuthor(author);
                        
         em.persist(lib);
         em.flush();
        }
        
        public String test(){
                return "This works";
        }
        
        public String getBookName(int id){
                Query q = em.createQuery("Select title from Books x Where x.id = :id").setParameter("id", id);
                return q.getSingleResult().toString();
                
        }

}

Thanks in advance for any suggestions.
Best Regards,
N.
[Message sent by forum member 'nupek' (nupek)]

http://forums.java.net/jive/thread.jspa?messageID=346662