This discussion is archived
3 Replies Latest reply: Apr 5, 2013 3:58 PM by Bruno Condemi RSS

Problem with EJB code

BK574 Newbie
Currently Being Moderated
Hi All,

I am trying to run the following code, but I am getting nullpointer exception.

1. RemoteInterface.java
______________________

package BeanPackage;

import javax.ejb.Remote;
@Remote

public interface RemoteInterface {
public String getName();
public String getNumber();
}

2. BeanImplementation.java
_____________________________

package BeanPackage;

import javax.ejb.EJB;
import javax.ejb.Stateless;

@Stateless
public class BeanImplementation implements RemoteInterface{

String name="abc";
String num="123123";


public String getName(){
return "Name of the employee is " +name;
}
public String getNumber(){
return "Number of the employee is" +num;
}
}

3. MainClass.java
_______________________________________

package BeanPackage;
import javax.ejb.EJB;

public class MainClass {

@EJB

private static RemoteInterface remoteinterfacebean;

public static void main (String[] args)

{

System.out.println(remoteinterfacebean.getName());

System.out.println(remoteinterfacebean.getNumber());

}
}

I am running this project inside JDeveloper. Let, me know what I did wrong. Appreciate your response.
  • 1. Re: Problem with EJB code
    939520 Explorer
    Currently Being Moderated
    I believe the problem is that EJB code must run in a Enterprise JavaBeans (EJB) container. You cant run it in a main function.
    The EJB container provides a run-time environment for enterprise beans within the application server. If your not familiar with
    such concepts already, I suggest you start out with writing servlets for the Tomcat server before messing with EJB.

    Edited by: 936517 on Mar 21, 2013 12:56 PM
  • 2. Re: Problem with EJB code
    gimbal2 Guru
    Currently Being Moderated
    Well your first mistake is saying "it doesn't work" without providing the actual error you are getting, and pointing out on which line of your code the error is happening.
    public class MainClass {
    
    @EJB
    private static RemoteInterface remoteinterfacebean;
    ...
    A server managed injection (@EJB) is never going to work in a client application; the client application is running far away from the server, possibly on a whole different computer in a whole different network on the other side of the planet. The fact that you're attempting it means you need to research more what this annotation actually does, or read about EJB technology to begin with. You can't learn this stuff by just copy/pasting code, it is very difficult material.

    What you want to do is research how you can do remote EJB lookups from a client application. Since you use JDeveloper I'm assuming you use Weblogic as the server. Try searching google for "weblogic ejb client lookup", or ask in the Weblogic forum if you can't understand the results you find.

    https://forums.oracle.com/forums/category.jspa?categoryID=193
  • 3. Re: Problem with EJB code
    Bruno Condemi Explorer
    Currently Being Moderated
    If you want to play with EJB the NetBeans IDE provides an embedded EJB container.

    Look at https://netbeans.org/kb/docs/javaee/javaee-entapp-junit.html

    Regards.

    Bruno Condemi

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points