0

i have a problem with my @inject annotation it can't instantiate my service. i follow every tutorial and official document without success. please can you help me to figure out my problem.

my controller: `

@Named
@ManagedBean
@RequestScoped
public class LoginAction {

    private Logger logger = LoggerFactory.getLogger( LoginAction.class );

    @Size(min = 5 , message = "#{msg.auth_txtLog_minLeng}")
    private String txtUserName;
    @Size(min = 5 , message = "#{msg.auth_txtPwd_minLeng}")
    private String txtPassWord;



    @Inject
    private UserService userService;

my service that call my DAO

  @Named
  public class UserServiceImpl implements UserService {

    @Inject
    UserDao userDao;

i get a nullPointerException:

> ATTENTION: #{loginAction.logIn}: java.lang.NullPointerException
javax.faces.FacesException: #{loginAction.logIn}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 23 more
Caused by: java.lang.NullPointerException
    at ma.trk.sgpa.controller.LoginAction.logIn(LoginAction.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 24 more
6 déc. 2013 11:37:43 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/sgpa-jsf] threw exception [java.lang.NullPointerException] with root cause
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
KhairiB
  • 45
  • 1
  • 8
  • What happens? Do you get an exception? You have to give us more information. – Keppil Dec 06 '13 at 11:11
  • Did you define your META-INF/beans.xml? – Keerthivasan Dec 06 '13 at 11:11
  • i define my beans.xml inn WEB-INf, i will move it to META-INf and i will notice you... i moved and it's not working – KhairiB Dec 06 '13 at 11:20
  • Since when can you annotate a bean with both `@ManagedBean` and `@Named`? If it's even possible, which package did the `@RequestScoped` come from? :S – Mr.J4mes Dec 06 '13 at 11:52
  • @Mr.J4mes the package came from javax.enterprise.context.RequestScoped and i tried to change javax.faces.bean.RequestScoped. but without effect – KhairiB Dec 06 '13 at 11:58
  • @TarikFakhouri: If you're using `javax.enterprise.context.RequestScoped`, just remove `@ManagedBean` annotation and try again. – Mr.J4mes Dec 06 '13 at 12:31
  • still not working. if i remove @ManagedBean my page Xhtml don't know my fields – KhairiB Dec 06 '13 at 14:06

1 Answers1

1

Stacktrace says that you're using Tomcat as indicated by org.apache.tomcat/coyote/catalina.* lines. However, Tomcat does as being a barebones JSP/Servlet container not natively support CDI, it has to be installed separately (like as you did for JSF).

Stacktrace also says that bean isn't managed by @Named but by @ManagedBean as indicated by complete absense of CDI proxy class signature in the root cause. If @Named was really used, you'd have seen an extra line as follows when Weld is used as CDI implementation:

Caused by: java.lang.NullPointerException
    at ma.trk.sgpa.controller.LoginAction.logIn(LoginAction.java:35)
--> at ma.trk.sgpa.controller.LoginAction$Proxy$_$$_WeldClientProxy.logIn(LoginAction$Proxy$_$$_WeldClientProxy.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    ...

Also, your comment

still not working. if i remove @ManagedBean my page Xhtml don't know my fields

confirms that CDI isn't properly installed at all. It's also surprising that you didn't explicitly mention anywhere how exactly you installed it because Tomcat+CDI is a special case, which confirms that you actually had no idea what you were doing. Perhaps you just faced a compilation error on @Named and @Inject and you just blindly downloaded/moved/copied random JAR files in a careless attempt to solve those compilation errors without actually reading the documentation of CDI implementation as to configuring Tomcat.

Head to the following answer to learn how to get CDI to work in Tomcat: How to install and use CDI on Tomcat?.

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • before i worked with CDI i read the official document from oracle and did some example with galssfish sever and jboss and it's worked fine. that why i want develop an application with tomcat, i didn't know that tomcat + CDI have a special case. anyway thank you i appreciate yout honesty. – KhairiB Dec 06 '13 at 15:49
  • 1
    GlassFish and JBoss are full fledged Java EE containers. They are full Java EE implementations which already bundles among others JSF, EJB, CDI, JPA, etc. Tomcat is a poor man's barebones JSP/Servlet container which does not bundle anything else than JSP/Servlet/EL. You have to install all other Java EE artifacts such as JSF, CDI, etc separately as JARs in `/WEB-INF/lib`. How else did you got the JSF code to compile and run on Tomcat? – BalusC Dec 06 '13 at 15:51
  • i use maven, so i added jsf-api and jsf-impl on pom.xml – KhairiB Dec 06 '13 at 15:57
  • That's one way. Maven automagically puts those libs in `/WEB-INF/lib` of build. By the way, in case you didn't got it, the question in my previous comment was purely rhetorical. – BalusC Dec 06 '13 at 16:00