Topic
  • 20 replies
  • Latest Post - ‏2013-07-05T09:20:33Z by STHV_Fabio_Marano
STHV_Fabio_Marano
STHV_Fabio_Marano
15 Posts

Pinned topic cdi @inject throws nullpointerexception

‏2013-06-14T14:00:59Z |

I've deployed an ear on websphere 8.5.

the application is composed of an ejb jar and a webapp ( Spring MVC ).

Here is my ejb :


@Stateless
public class DiscrepanciesServiceImpl implements DiscrepanciesService {

    @Inject
    private DiscrepancyDao discrepancyDao;
    
    public DiscrepanciesServiceImpl(){};

    public List<Discrepancy> viewDiscrepancies() {
    
                return discrepancyDao.findAll();
    }

}

and when I invoke viewDiscrepancies() i get nullpointerexception on discrepancyDao ( injected by CDI  ).

I've attached a zipped trace-log based on the configuration as follows : *=info: JCDI=all: com.ibm.ws.webbeans*=all: org.apache.webbeans*=all: EJBContainer=all: MetaData=all: Injection=all as seen in infocenter.

Thanks in advance for your help.

Fabio

Attachments

Updated on 2013-06-14T14:25:19Z at 2013-06-14T14:25:19Z by STHV_Fabio_Marano
  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts
    ACCEPTED ANSWER

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-20T06:05:51Z  
    • bpaskin
    • ‏2013-06-19T18:07:55Z

    Hi, I just looked at your code.  I do not see you initializing the EntityManager in your code. 

    Regards,

    Brian

    Hi, 

    thanks for sending me the app. However now I resolved the issue. It was a classloading issue due to a hibernate wrong configuration. Now it's works fine. 

    Regards

    Fabio

    Updated on 2013-06-20T06:05:58Z at 2013-06-20T06:05:58Z by STHV_Fabio_Marano
  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts
    ACCEPTED ANSWER

    Re: cdi @inject throws nullpointerexception

    ‏2013-07-05T09:20:33Z  

    Out of curiosity, can you summarize what happened?  So injection was occuring just fine all along, but spring was satisfiyng the injection point w/ something not usable?

    it was a simply classloader issue with Hibernate. Spring works fine and cdi also. 

    Regards 

    Fabio

  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-14T19:46:10Z  

    Hi, It works for me correctly using 8.5.0.2.  Did you place the beans.xml file in the correct place?  I am attaching my sample app.

    Access it http://host:port/CDIExample85/random

    Regards,

    Brian

    Attachments

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-14T20:02:53Z  
    • bpaskin
    • ‏2013-06-14T19:46:10Z

    Hi, It works for me correctly using 8.5.0.2.  Did you place the beans.xml file in the correct place?  I am attaching my sample app.

    Access it http://host:port/CDIExample85/random

    Regards,

    Brian

    hi infact the problem is  inside discrepancyDaoImpl ( discrepancyDao is interface )

     @PersistenceContext(unitName = "MEV_PU")
        protected EntityManager em;
     
        private Class<T> type;
     
        @SuppressWarnings("unchecked")
        public _GenericDaoImpl() {
     
            Type t = getClass().getGenericSuperclass();
            ParameterizedType pt = (ParameterizedType) t;
            type = (Class<T>) pt.getActualTypeArguments()[0];
            System.out.println("///////////////////////////////////////////////JPA Provider/////////////");
            System.out.println("///////////////////////////////////////////////"+em.getEntityManagerFactory().getClass());
        }
    if I test EntityManager with Null returns false, but when I invoke em.getEntityManagerFactory().getClass() returns NullPointerException.

    I don't if you can help me , I guess to have a problem with Hibernate my PU is as follow

    <?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="MEV_PU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>MevaluateDatasource</jta-data-source>  
    <jar-file>MEV_Domain-0.0.1-SNAPSHOT.jar</jar-file>
    <properties>
    <property name="hibernate.transction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory" />
    <property name="hibernate.transction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
    <property name="jta.UserTransaction" value="java:comp/UserTransaction" />
      
    <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform"/>
     
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
       <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>
    </persistence-unit>
    </persistence>

    could you please send some sort of help ?

    Thanks

    Regards

    Fabio

  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-17T11:43:41Z  

    hi infact the problem is  inside discrepancyDaoImpl ( discrepancyDao is interface )

     @PersistenceContext(unitName = "MEV_PU")
        protected EntityManager em;
     
        private Class<T> type;
     
        @SuppressWarnings("unchecked")
        public _GenericDaoImpl() {
     
            Type t = getClass().getGenericSuperclass();
            ParameterizedType pt = (ParameterizedType) t;
            type = (Class<T>) pt.getActualTypeArguments()[0];
            System.out.println("///////////////////////////////////////////////JPA Provider/////////////");
            System.out.println("///////////////////////////////////////////////"+em.getEntityManagerFactory().getClass());
        }
    if I test EntityManager with Null returns false, but when I invoke em.getEntityManagerFactory().getClass() returns NullPointerException.

    I don't if you can help me , I guess to have a problem with Hibernate my PU is as follow

    <?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="MEV_PU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>MevaluateDatasource</jta-data-source>  
    <jar-file>MEV_Domain-0.0.1-SNAPSHOT.jar</jar-file>
    <properties>
    <property name="hibernate.transction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory" />
    <property name="hibernate.transction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
    <property name="jta.UserTransaction" value="java:comp/UserTransaction" />
      
    <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform"/>
     
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />
       <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>
    </persistence-unit>
    </persistence>

    could you please send some sort of help ?

    Thanks

    Regards

    Fabio

    Hi,

    There are probably some information in the SystemOut.log and FFDCs when this error occurs.  If you attach those files, I'll take a look.

    Regards,

    Brian

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-17T12:14:30Z  
    • bpaskin
    • ‏2013-06-17T11:43:41Z

    Hi,

    There are probably some information in the SystemOut.log and FFDCs when this error occurs.  If you attach those files, I'll take a look.

    Regards,

    Brian

    Hi 

    Thanks in advance for your help. in the SystemOut.Log start your look at [17/06/13 14.02.35:993 CEST] , same on the trace.log  (enabled for *=info: JPA=all ) 

    Regars 

    Fabio

    Attachments

    Updated on 2013-06-17T12:15:48Z at 2013-06-17T12:15:48Z by STHV_Fabio_Marano
  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-17T12:57:14Z  

    Hi 

    Thanks in advance for your help. in the SystemOut.Log start your look at [17/06/13 14.02.35:993 CEST] , same on the trace.log  (enabled for *=info: JPA=all ) 

    Regars 

    Fabio

    Hi, Thanks for sending the logs.  There are a few interesting things in the logs.  Are you deploying this from RAD?

    There are a few errors about the BLA, which indicate that you may not have the correct permission to read/write to the directory correctly, or there is a configuration inconsistency.  What is on line 35 of DiscrepanciesServiceImpl.java?   Also, which level of EJB are you coding (2.0, 2.1, etc)?  It appears that the EJB may not be following the specification. 

    Regards,

    Brian

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-17T14:03:16Z  
    • bpaskin
    • ‏2013-06-17T12:57:14Z

    Hi, Thanks for sending the logs.  There are a few interesting things in the logs.  Are you deploying this from RAD?

    There are a few errors about the BLA, which indicate that you may not have the correct permission to read/write to the directory correctly, or there is a configuration inconsistency.  What is on line 35 of DiscrepanciesServiceImpl.java?   Also, which level of EJB are you coding (2.0, 2.1, etc)?  It appears that the EJB may not be following the specification. 

    Regards,

    Brian

    Hi, Thanks for the reply. 

    1) I'm deplooying from admin console of was 8.5.

    2) on line 35 there is "return discrepancyDao.findAll() whom parent class  implements as follows:

      public List<T> findAll() {
             
            CriteriaQuery<T> query = this.getCriteriaQuery();
            Root<T> fromEntities = query.from(type);
            query.select(fromEntities);
     
            return em.createQuery(query).getResultList();
        }
    where I think that em ( EntityManager ) is null and not correctly injected by the WAS.

    3) I'm coding ejb 3.0 specification.

    Regards,

    Fabio

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-17T14:07:23Z  

    Hi, Thanks for the reply. 

    1) I'm deplooying from admin console of was 8.5.

    2) on line 35 there is "return discrepancyDao.findAll() whom parent class  implements as follows:

      public List<T> findAll() {
             
            CriteriaQuery<T> query = this.getCriteriaQuery();
            Root<T> fromEntities = query.from(type);
            query.select(fromEntities);
     
            return em.createQuery(query).getResultList();
        }
    where I think that em ( EntityManager ) is null and not correctly injected by the WAS.

    3) I'm coding ejb 3.0 specification.

    Regards,

    Fabio

    Sorry I'm coding within a Indigo Eclipse Client on Windows 7 Professional x64

    Regards 

    Fabio

  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-17T14:53:55Z  

    Sorry I'm coding within a Indigo Eclipse Client on Windows 7 Professional x64

    Regards 

    Fabio

    Hi, can you check your to make sure that you have a correct dependency injection for Spring?

    From the trace log, it does not show that the entitymanager itself is null. So, it is hard to say exactly where is your problem.  If you check your Spring configuration and that is fine, then I validate that the variable em (EntityManager) is null.

    Regards,

    Brian

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-17T15:06:42Z  
    • bpaskin
    • ‏2013-06-17T14:53:55Z

    Hi, can you check your to make sure that you have a correct dependency injection for Spring?

    From the trace log, it does not show that the entitymanager itself is null. So, it is hard to say exactly where is your problem.  If you check your Spring configuration and that is fine, then I validate that the variable em (EntityManager) is null.

    Regards,

    Brian

    Hi yes I've a correct DI for spring because it loads as follows in the web.xml

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-service.xml</param-value>
        </context-param>

    and my spring-service.xml is as follows :

     
        <jee:local-slsb id="discrepanciesServiceEjb"
            jndi-name="ejblocal:com.mevaluate.domain.facades.DiscrepanciesService"
            business-interface="com.mevaluate.domain.facades.DiscrepanciesService" />

    As you can see I would not have a way to invoke the ejb without spring

    Thanks 

    Regards

    Fabio

     

     
  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-17T16:20:09Z  

    Hi yes I've a correct DI for spring because it loads as follows in the web.xml

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-service.xml</param-value>
        </context-param>

    and my spring-service.xml is as follows :

     
        <jee:local-slsb id="discrepanciesServiceEjb"
            jndi-name="ejblocal:com.mevaluate.domain.facades.DiscrepanciesService"
            business-interface="com.mevaluate.domain.facades.DiscrepanciesService" />

    As you can see I would not have a way to invoke the ejb without spring

    Thanks 

    Regards

    Fabio

     

     

    Hi 

    I've created a remote interface on the ejb to bypass web and spring DI.

    Now is confirmed during the debug that EntityManager is null

    I've aso tested the null of the @Inject annotated component.

    So it seems that the server ingnore any type of  injection.

    I'm attaching the source code of my application ( Ejb, Dao )

    Regards

    Fabio

    Updated on 2013-06-17T17:53:31Z at 2013-06-17T17:53:31Z by STHV_Fabio_Marano
  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-18T01:27:19Z  

    Hi yes I've a correct DI for spring because it loads as follows in the web.xml

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-service.xml</param-value>
        </context-param>

    and my spring-service.xml is as follows :

     
        <jee:local-slsb id="discrepanciesServiceEjb"
            jndi-name="ejblocal:com.mevaluate.domain.facades.DiscrepanciesService"
            business-interface="com.mevaluate.domain.facades.DiscrepanciesService" />

    As you can see I would not have a way to invoke the ejb without spring

    Thanks 

    Regards

    Fabio

     

     

    Hi,

    I was able to put  a little example, however, without Spring.  I am able to get this to work correctly.  Can you make sure you have the beans.xml in the correct place.

    Thanks, Brian

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-18T08:14:58Z  
    • bpaskin
    • ‏2013-06-18T01:27:19Z

    Hi,

    I was able to put  a little example, however, without Spring.  I am able to get this to work correctly.  Can you make sure you have the beans.xml in the correct place.

    Thanks, Brian

    Hi, 

    Yes the beans.xml is in the in META-INF of ejb jar. 

    however, to me it worked but only after installing a new application with an ear ejb stateless with the same PU. After that I updated the application by entering into the ear the  ejb and war. Only in this way it works. Must I create a new profile of was? Because the fact remains that if I deploy only the ear as described above ( that is with only the ejb jar and war ) it does not work for me.

    Thanks Regards,

    Fabio

  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-18T11:47:04Z  

    Hi, 

    Yes the beans.xml is in the in META-INF of ejb jar. 

    however, to me it worked but only after installing a new application with an ear ejb stateless with the same PU. After that I updated the application by entering into the ear the  ejb and war. Only in this way it works. Must I create a new profile of was? Because the fact remains that if I deploy only the ear as described above ( that is with only the ejb jar and war ) it does not work for me.

    Thanks Regards,

    Fabio

    Hi, that is rather strange, however,  I did mention above that there seems to be some type of corruption in your current environment.  Adding another application should have no effect on another application, unless there is some classloader issues.  If you want to use your current Profile, I would do the following:

    1. Uninstall the applications

    2. Make sure the business level artifacts are deleted

    3. Delete any remnants in the <profile_home>/config/cells/<cell_name>/applications

    4. do the same as 3, but in the  /blas directory

    5. do the same as 3, but in the  /cus directory

    6. clear <profile_home>/temp

    7. clear <profile_home>/wstemp

    Regards,

    Brian

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-18T17:18:57Z  
    • bpaskin
    • ‏2013-06-18T11:47:04Z

    Hi, that is rather strange, however,  I did mention above that there seems to be some type of corruption in your current environment.  Adding another application should have no effect on another application, unless there is some classloader issues.  If you want to use your current Profile, I would do the following:

    1. Uninstall the applications

    2. Make sure the business level artifacts are deleted

    3. Delete any remnants in the <profile_home>/config/cells/<cell_name>/applications

    4. do the same as 3, but in the  /blas directory

    5. do the same as 3, but in the  /cus directory

    6. clear <profile_home>/temp

    7. clear <profile_home>/wstemp

    Regards,

    Brian

    Hi

    I installed a new profile and the ear doesn't work. 

    Regards 

    Fabio

  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-19T11:38:19Z  

    Hi

    I installed a new profile and the ear doesn't work. 

    Regards 

    Fabio

    Hi, I am looking at this now.  I am going to create a sample CDI inject and if that works, expand it to JPA.  Though, both worked for me recently.

    Regards,

    Brian

  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-19T15:50:39Z  

    Hi

    I installed a new profile and the ear doesn't work. 

    Regards 

    Fabio

    Hi, I was able to create an example with Injection of EJBs.  It uses JPA and a servlet to access the data, which is stored in a Derby DB.  I did not use Hibernate or Spring.  The example runs fine.  You can access the example using http://host:port/CDIEJBExampleWeb85/

    Regards,

    Brian

  • bpaskin
    bpaskin
    4212 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-19T18:07:55Z  

    Hi

    I installed a new profile and the ear doesn't work. 

    Regards 

    Fabio

    Hi, I just looked at your code.  I do not see you initializing the EntityManager in your code. 

    Regards,

    Brian

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-20T06:05:51Z  
    • bpaskin
    • ‏2013-06-19T18:07:55Z

    Hi, I just looked at your code.  I do not see you initializing the EntityManager in your code. 

    Regards,

    Brian

    Hi, 

    thanks for sending me the app. However now I resolved the issue. It was a classloading issue due to a hibernate wrong configuration. Now it's works fine. 

    Regards

    Fabio

    Updated on 2013-06-20T06:05:58Z at 2013-06-20T06:05:58Z by STHV_Fabio_Marano
  • Eric Covener
    Eric Covener
    79 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-06-22T18:09:08Z  

    Hi, 

    thanks for sending me the app. However now I resolved the issue. It was a classloading issue due to a hibernate wrong configuration. Now it's works fine. 

    Regards

    Fabio

    Out of curiosity, can you summarize what happened?  So injection was occuring just fine all along, but spring was satisfiyng the injection point w/ something not usable?

  • STHV_Fabio_Marano
    STHV_Fabio_Marano
    15 Posts

    Re: cdi @inject throws nullpointerexception

    ‏2013-07-05T09:20:33Z  

    Out of curiosity, can you summarize what happened?  So injection was occuring just fine all along, but spring was satisfiyng the injection point w/ something not usable?

    it was a simply classloader issue with Hibernate. Spring works fine and cdi also. 

    Regards 

    Fabio