0

I am calling my Ejb bean from My Test class.In test class i am using static keyword in front of here userService because i am accessing the userService from the static context(from main method.)

Note: Also If i donot uses the static keyword in front of userService then also i got same error.

public class Test {

    @Inject 
    private static UserService userService;

    public Test() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String args[]){
        userService.doSomething("hello");
    }

}

I setup an EJB 3 interface/implementation looking like this...

UserService (interface)

package business;

public interface UserService {

    public String doSomething();

}

UserServiceBean (implementation)

@Stateless
@Local
public class UserServiceBean implements UserService{

    public UserServiceBean() {
    }

    @Override
    public String doSomething() {
        return "Work done!";
    }

}

When i am calling doSomething method of UserServiceBean class using interface from my Test class. Its giving me error like this.

Exception in thread "main" java.lang.NullPointerException
    at test.Test.main(Test.java:17)

Please explain me why this error is coming.

  • `userService` is null, and nothing is assigning it a value. From the `@Inject` annotation I assume you *expected* something to do so, but it's not clear to me that you've actually configured such behavior. I would not expect, for instance, a `static` field to be injectable. – dimo414 Nov 28 '16 at 09:19
  • I used static here because i am accessing the userService from the static context. Or purpose of injecting the UserService is just to get the output from EJB bean(UserServiceBean ) by calling it. My question is very clear. –  Nov 28 '16 at 09:24
  • What container you are using? – HRgiger Nov 28 '16 at 09:32
  • Its not duplicate open your eyes. It is another case with different solution. It is only comes around nullpointer. @rkosegi –  Nov 28 '16 at 10:03

1 Answers1

0

You are getting Null pointer exception because userService in class Test is static. Injection does not works for static fields. Remove static keyword from field userService. define it like

 @Inject 
 private  UserService userService;

Reference: https://blogs.oracle.com/chengfang/entry/do_s_and_don_ts

Refer this link for how to invoke the service method: http://www.coderpanda.com/ejb-stateless-session-bean-example/

Reena Upadhyay
  • 1,977
  • 20
  • 35
  • After removing the static key word it also giving nullPointer error. –  Nov 28 '16 at 09:29
  • can you give me a example for more explanation. –  Nov 28 '16 at 09:31
  • Please share your entire piece of code. You can update your question to add the entire code. If you have any configuration file, then do share that also. – Reena Upadhyay Nov 28 '16 at 09:33
  • Can you refer the this link http://www.coderpanda.com/ejb-stateless-session-bean-example/ There might be some code issue when you are invoking the method – Reena Upadhyay Nov 28 '16 at 09:41
  • Thanks for your link @reena . But even i didnot find the answer. I also used your getter setter method it also not working. For project configuration i simply made a dynamic project then make only these 3 classes which i have wrote to just only test my output for learning purpose. –  Nov 28 '16 at 10:02
  • @bharti "*removing the static key word it also giving nullPointer error*" well yes, you can't just blindly delete things and expect your code to suddenly work. *Something* must assign `userService` a non-null value, otherwise you'll get an NPE. What, exactly, do you expect to assign it a value, and where in your code is that happening? – dimo414 Nov 28 '16 at 18:31