I’ve two backing beans:
Login: a bean that validates the user login info (username and password) with database table.Buss_Services: another bean that performs some business services.
I need to get the user ID from the Login bean and use it inside the Buss_Services. It’s stored in a String property of Login and the Buss_Services needs this value to track the currently logged-in user and update the DB.
Here’s the Login backing bean:
@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);
}
}
Here’s the Buss_Services backing bean which calls the getLoggedID() method:
@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.
}
This is the stack trace which I get when I try to access the bussiness services page:
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
How is this caused and how can I solve it?
UPDATE :
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:
- add the
<managed-property>of login to the adfc-config.xml file . - more importantly , obtain the
loggedUserIDinside a@PostConstructanotated method init() . Thanx everyone.
A
NullPointerExceptionon that line has as the only possible cause thatloginisnull. Given the fact that the@ManagedPropertylooks fine, this can have only one possible cause: the setter methodsetLogin()is broken. Make sure that it look exactly like this:and thus not
or something else.
Update as per the comments:
Finally, there is the cause of your problem. Configuration in
faces-config.xmloverrides all JSF2 annotations on the bean in question. You have apparently not configured the<managed-property>in thefaces-config.xml. You have 2 options:Remove the whole
<managed-bean>configuration infaces-config.xml. The whole point of new JSF 2.x annotations like@ManagedBean,@ManagedProperty, etc is to get rid of verbose JSF 1.x style XML configuration.Add a
<managed-property>value of#{Login}to the<managed-bean>ofBuss_Services.Unrelated to the concrete problem. You’ve several serious flaws in the design and code style.
You should not reference
UIComponents as properties. Instead, you should reference its values. Keep the model as simple as possible and never useUIComponentunless you have a really valid reason. E.g.Your login validation method is inefficient. It seems to haul the entire users table from DB into Java’s memory wherein you test every individual row. You should try to write and finetune the SQL query as much as possible so that it returns exactly the information you’re looking for.
Your PHP-like code style fully contradicts the Java Naming Conventions. It makes the code harder to read and maintain by all other Java developers, such as the ones from who you expect answers when you post the code on the Internet, like here. Package names should be all lowercase. Underscores are only valid in constants, for all other names CamelCase should be used. Instance names (like managed bean names) should start with lowercase. Etcetera.