1 Reply Latest reply on Apr 9, 2008 3:33 AM by Jeroen Wenting

    First EBJ: Session Bean, Eclipse, JBOSS AS

    Joe Royall Newbie

      I am having trouble with a basic session bean. My understanding is that I don't need to initiate a session bean object because the EE framework will do it for me. Am I missing anything in my config?

      Eclipse: Version: 3.3.1.1
      WST 2.0.2
      J2ee Standard Tools 2.0.2
      JBOSS AS: 4.2


      1. File --> New --> Project... --> EJB --> EJB Project
      Target Runtime JBoss v4.2
      Default Configuration for JBoss v4.2
      Project Facet EBJ Module 3.0 Java 5.0

      2. Create package
      com.example.helloworld

      3. Create interface -> HelloSessionLocal.java

      package com.example.helloworld;

      import javax.ejb.Local;

      @Local
      public interface HelloSessionLocal {
      public void hello();

      }

      4. Create class HelloSession.java

      package com.example.helloworld;

      import javax.ejb.Stateless;

      @Stateless
      public class HelloSession implements HelloSessionLocal {

      public HelloSession(){
      }
      public void hello(){
      System.out.println("hello from ejb stateless bean");
      }
      }


      5. New --> Project... --> Dynamic Web Project
      HelloServlet

      6. Properties --> J2EE Module Dependencies --> HelloWorld

      7. New --> Other --> Web --> Servlet
      HelloServlet.java

      package com.example.helloservlet;

      import java.io.IOException;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import com.example.helloworld.*;

      import javax.ejb.EJB;

      public class HelloServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
      @EJB
      public static HelloSession helloSession;
      static final long serialVersionUID = 1L;


      public HelloServlet() {
      super();
      }


      public void init() throws ServletException {
      System.out.println("hello servlet");
      helloSession = new HelloSession();
      helloSession.hello();
      super.init();
      }
      }


      8. Run as --> Jboss 4.2 Server --> Configured Projects HelloServlet, HelloWorld

      9. Console Output
      16:13:35,727 INFO [STDOUT] hello servlet
      16:13:35,747 ERROR [[/HelloServlet]] StandardWrapper.Throwable
      java.lang.NullPointerException

      If I instiate HelloSession with helloSession = new HelloSession(); in main

      then console output is as expected
      116:15:21,516 INFO [STDOUT] hello servlet
      16:15:21,522 INFO [STDOUT] hello from ejb stateless bean