title: NullPointerException Error when trying to get a value from Another Bean in JSF
body: <p>I've two backing beans:</p>
  
  <ul>
  <li><code>Login</code>: a bean that validates the user login info (username and password) with database table.</li>
  <li><code>Buss_Services</code>: another bean that performs some business services.</li>
  </ul>
  
  <p>I need to get the user ID from the <code>Login</code> bean and use it inside the <code>Buss_Services</code>. It's stored in a String  property of <code>Login</code> and the <code>Buss_Services</code> needs this value to track the currently logged-in user and update the DB.</p>
  
  <p>Here's the <code>Login</code> backing bean:</p>
  
  <pre><code>@ManagedBean(name="Login")
  @SessionScoped
  public class Login {
      private String loggedUserID;
  
      public Object logCB_action() {
          try {
              // ...
              rs = stmt.executeQuery(SQL);
              while (rs.next()) {
                  if (rs.getString("USER_NAME").equals(uname)) {
                      if (rs.getString("USER_PW").equals(pword)) {
  
                       // Here, the user ID is set.
                      loggedUserID=rs.getString("USER_ID");// This line ...
  
  
                      System.out.println("Logged User (ID): "+ userID);
  
                          return ("displayApp");
                      }
                  }
              }
          }
  
          // ...
      }        
  
      public String getLoggedID() {
          // Here, the user ID is returned.
          String id = loggedUserID;
          return (id);
      }
  
  }
  </code></pre>
  
  <p>Here's the <code>Buss_Services</code> backing bean which calls the <code>getLoggedID()</code> method:</p>
  
  <pre><code>@ManagedBean(name="Buss_Services")
  @SessionScoped
  public class Buss_Services {
  
  
  
      @ManagedProperty("#{Login}")
      private Login login;
  
      public void newEst_action() {
          // The following line throws NullPointerException.
          System.out.println("Logged User (ID): " + login.getLoggedID());       
      }
  
      // Getters/setters.
  }
  </code></pre>
  
  <p>This is the stack trace which I get when I try to access the bussiness services page:</p>
  
  <pre><code>javax.faces.el.EvaluationException:
  //C:/Users/Sultan09/AppData/Roaming/JDeveloper/system11.1.2.0.38.60.17/o.j2ee/drs/TheOCES/OCES.ViewControllerWebApp.war/App_Business_SerivesPG.jsf @68,140 action="#{backingBeanScope.App_BServPG_Bean.newEst_action}": java.lang.NullPointerException
  
  at org.apache.myfaces.trinidad.component.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:51)
  at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
  at org.apache.myfaces.trinidad.component.UIXCommand.broadcast(UIXCommand.java:190)
  at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:787)
  at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1252)
  at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._invokeApplication(LifecycleImpl.java:965)
  at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl._executePhase(LifecycleImpl.java:346)
  at oracle.adfinternal.view.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:204)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
  at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
  at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
  at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
  at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
  at oracle.adf.model.servlet.ADFBindingFilter.doFilter(ADFBindingFilter.java:173)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
  at oracle.adfinternal.view.faces.webapp.rich.RegistrationFilter.doFilter(RegistrationFilter.java:121)
  at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:468)
  at oracle.adfinternal.view.faces.activedata.AdsFilter.doFilter(AdsFilter.java:60)
  at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl$FilterListChain.doFilter(TrinidadFilterImpl.java:468)
  at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:293)
  at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:199)
  at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:92)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
  at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:111)
  at java.security.AccessController.doPrivileged(Native Method)
  at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:313)
  at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:413)
  at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:94)
  at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:161)
  at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:71)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
  at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:136)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
  at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
  at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3715)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3681)
  at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
  at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
  at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2277)
  at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2183)
  at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1454)
  at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
  at weblogic.work.ExecuteThread.run(ExecuteThread.java:178
  
   Caused by: java.lang.NullPointerException
      at JavaView.backing.Buss_Services.newEst_action(Buss_Services.java:172)
      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 com.sun.el.parser.AstValue.invoke(Unknown Source)
      at com.sun.el.MethodExpressionImpl.invoke(Unknown Source)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
      at org.apache.myfaces.trinidad.component.MethodExpressionMethodBinding.invoke(MethodExpressionMethodBinding.java:46)
      ... 44 more
  </code></pre>
  
  <p>How is this caused and how can I solve it?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong> :
  As per comments here and personal search on similar issues to the one stated here, the Problem is Finally solved ,thank god.
  The solution was that I had to:</p>
  
  <ol>
  <li>add the <code>&lt;managed-property&gt;</code> of login to the adfc-config.xml file . </li>
  <li>more importantly , obtain the <code>loggedUserID</code> inside a <code>@PostConstruct</code> anotated method init() . Thanx everyone.</li>
  </ol>