Discussions

EJB programming & troubleshooting: Accessing an ArrayList from a Servlet

  1. Accessing an ArrayList from a Servlet (5 messages)

    I have a probelm with passing an ArrayList to a Servlet. I use a session bean.
    When my business method refers to a single object the everything works fine:

    public util.CustDetails aut(int id) throws javax.ejb.FinderException {
            CustomerTblLocal cc = custHome.findByPrimaryKey(new Integer(id));
            return new util.CustDetails(cc.getCustomerNum(), cc.getCity(), cc.getName());
        }

    When my business method refers to an ArrayList or Collection of objects then I get an error message

    public java.util.ArrayList kkk(java.lang.String city) throws javax.ejb.FinderException,java.rmi.RemoteException {
            ArrayList list = null;
            Collection m = custHome.findByCity("Miami");
            Iterator i = m.iterator();
            while(i.hasNext()){
                CustomerTblLocal cc = (CustomerTblLocal)i.next();
                list.add(new util.CustDetails(cc.getCustomerNum(), cc.getName(),cc.getCity()));
            }
            return list;
        }

    The error is seems to happen when I try to get the ArrayList in the servlet with the code:

    java.util.ArrayList list = f.kkk("Miami");

    Can you possibly figure out what is going on?

    The server's log is :

    [#|2005-06-18T13:13:54.582+0100|INFO|sun-appserver-pe8.1_02|javax.enterprise.system.container.ejb|_ThreadID=22;|
    javax.ejb.EJBException
    at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:2807)
    at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:2713)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:2521)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:819)
    at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:137)
    at $Proxy133.kkk(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:585)
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:167)
    at com.sun.corba.ee.impl.presentation.rmi.bcel.BCELStubBase.invoke(Unknown Source)
    at ejb._FaceRemote_DynamicStub.kkk(_FaceRemote_DynamicStub.java)
    at web.NewServlet.processRequest(NewServlet.java:42)
    at web.NewServlet.doGet(NewServlet.java:63)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:747)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
    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:585)
    at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
    at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
    at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:257)
    at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)
    at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:263)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:173)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:132)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:933)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:185)
    at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:653)
    at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:534)
    at com.sun.enterprise.web.connector.grizzly.ProcessorTask.doTask(ProcessorTask.java:403)
    at com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:55)
    Caused by: java.lang.NullPointerException
    at ejb.FaceBean.kkk(FaceBean.java:107)
    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:585)
    at com.sun.enterprise.security.SecurityUtil$2.run(SecurityUtil.java:153)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.enterprise.security.application.EJBSecurityManager.doAsPrivileged(EJBSecurityManager.java:950)
    at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:158)
    at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:128)
    ... 41 more
    |#]

    [#|2005-06-18T13:13:54.584+0100|SEVERE|sun-appserver-pe8.1_02|javax.enterprise.system.container.web|_ThreadID=22;|StandardWrapperValve[NewServlet]: Servlet.service() for servlet NewServlet threw exception
    java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
    java.rmi.RemoteException
    at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.wrapException(Util.java:657)
    at javax.rmi.CORBA.Util.wrapException(Util.java:279)
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:177)
    at com.sun.corba.ee.impl.presentation.rmi.bcel.BCELStubBase.invoke(Unknown Source)
    at ejb._FaceRemote_DynamicStub.kkk(_FaceRemote_DynamicStub.java)
    at web.NewServlet.processRequest(NewServlet.java:42)
    at web.NewServlet.doGet(NewServlet.java:63)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:747)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:860)
    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:585)
    at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
    at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
    at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:257)
    at org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:55)
    at org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:161)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:263)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:173)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:132)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:551)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:933)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:185)
    at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:653)
    at com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:534)
    at com.sun.enterprise.web.connector.grizzly.ProcessorTask.doTask(ProcessorTask.java:403)
    at com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:55)
    Caused by: java.rmi.RemoteException
    |#]
  2. You return null ![ Go to top ]

    ArrayList list = null;
    ...
       // lot of statement not setting list
    ...

    return list;
  3. But I thought that this line could do the job.

    list.add(new util.CustDetails(cc.getCustomerNum(), cc.getName(),cc.getCity()));

    It is true that it returns a null pointer exception.

    But why something like this happens?
  4. I think you need to initiate ArrayList list.
    Hope this works fine.
  5. It's ok now.
    I just needed to check if the object added to the list is null.

    if (object!=null){
        list.add(object);
    }

    This way it works.
  6. Just curious[ Go to top ]

    Why haven't you something like:

    return custHome.findCustomerDetailsByCity("Miami");

    :)