6 Replies Latest reply on Sep 21, 2012 6:34 AM by Nicklas Karlsson

    StatesLess EJB is not Injected from controller and the value is null using J2EE 6 CDI Feature

    Sree . Newbie

      Hi,

       

      The templates are not the issue as templates are working fine. I have added beanx.xml now, constructor is getting invoked.

       

      StatesLess EJB is not Injected and the value is null. Iam trying to deploy everything in one war file. Let me know if Iam missing any thing.

       

      iam getting null pointer exception beacuse StatlessLess EJB is null.

       

       

       

      ===================================================

       

      Caused by: java.lang.NullPointerException
      at com.ibytecode.businesslogic.controller.ActiorController.<init>(ActiorController.java:69) [classes:]
      at com.ibytecode.businesslogic.controller.ActiorController$Proxy$_$$_WeldClientProxy.<init>(ActiorController$Proxy$_$$_WeldClientProxy.java) [classes:]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.6.0_13]
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [rt.jar:1.6.0_13]
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [rt.jar:1.6.0_13]
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [rt.jar:1.6.0_13]
      at java.lang.Class.newInstance0(Class.java:355) [rt.jar:1.6.0_13]
      at java.lang.Class.newInstance(Class.java:308) [rt.jar:1.6.0_13]
      at org.jboss.weld.util.reflection.SecureReflections$16.work(SecureReflections.java:343) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
      at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
      at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:173) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]

       

      ========================================================================================================================

       

      Pl check the following of sourcecode xhtml, Controller and StatelessEJB. Pl also check Attached sourceCode.

       

      Controller:

       

      package com.ibytecode.businesslogic.controller;

      import java.io.Serializable;

      import javax.ejb.EJB;
      import javax.enterprise.context.SessionScoped;
      import javax.inject.Named;

      import com.ibytecode.businesslogic.ActorBean;
      import com.ibytecode.entity.Actor;

      @Named(value = "actorService")
      @SessionScoped
      public class ActorController implements Serializable {

      /**
      * Injected GreeterEJB client
      */
      @EJB
      private ActorBean actorEJB;

      private String greeting;

      public String getGreeting() {

      return greeting;
      }

      public ActorController() {

      Actor actor = new Actor();
      actor.setActorId(new Short("201"));
      actor = actorEJB.findActor(actor);

      if (actor != null) {
      greeting = "Hello, " + actor.getFirstName() + " "
      + actor.getLastName() + "!";
      } else {
      greeting = "No such user exists! Use 'emuster' or 'jdoe'";
      }
      }
      }

       

      =============================================

      XHTML

       

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      >

       

      <h:body>
      <ui:composition template="template/common/template.xhtml">
      <ui:define name="content">
      <h:messages />
      <h:outputText value="#{actorService.greeting}"/>
      </ui:define>
      </ui:composition>
      </h:body>
      </html>

       

       

       

      =========================================================

       

       

       

      package com.ibytecode.businesslogic;

      import java.util.List;

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

      import com.ibytecode.entity.Actor;

      @Stateless
      public class ActorBean {

      public ActorBean() { }

      @PersistenceContext(unitName = "FirstEJBsProject")
      private EntityManager entityManager;

      private String greeting;

      public String getGreeting() {
      return greeting;
      }

      public void setGreeting(String greeting) {
      this.greeting = greeting;
      }

      public void saveActor(Actor actor) {

      entityManager.persist(actor);

      }

      public Actor findActor(Actor actor) {

      Actor p = entityManager.find(Actor.class, actor.getActorId());

      return p;

      }

      public List<Actor> retrieveAllActors() {

      String q = "SELECT p from " + Actor.class.getName() + " p";

      Query query = entityManager.createQuery(q);

      List<Actor> actors = query.getResultList();

      return actors;

      }

      }

       

      Early response would be appreciated.

       

      Thanks,

      Sree