-2

I checked many places, but unable to find the error in my configuration. Please help.

faces-config.xml

<managed-bean>
    <managed-bean-name>notificationServlet</managed-bean-name>
    <managed-bean-class>com.comviva.workflow.ui.notification.NotificationServlet</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>notificationDao</property-name>
        <value>#{notificationDaoService}</value>
    </managed-property>
</managed-bean>

applicationContext.xml

<bean id="notificationDao" class="com.comviva.workflow.ui.dao.NotificationDaoImpl">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="notificationDaoService"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager">
        <ref local="myTransactionManager" />
    </property>
    <property name="target">
        <ref local="notificationDao" />
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="*">PROPAGATION_REQUIRED</prop>
            <prop key="get*">PROPAGATION_NEVER</prop>
        </props>
    </property>
</bean>

Java Class

@SessionScoped
@ManagedBean(name = "notificationServlet")
public class NotificationServlet extends HttpServlet
{

private static final long serialVersionUID = -3905661117247475401L;

@ManagedProperty("#{notificationDao}")
NotificationDao notificationDao;
}

I am getting NullPointerException. Please suggest me what I am missing.

Edit:-

2013-08-13 21:22:34,407 INFO  (NotificationServlet.java:43) notificationDao :: null
2013-08-13 21:22:34,407 ERROR (NotificationServlet.java:60) Exception ::
java.lang.NullPointerException
    at com.comviva.workflow.ui.notification.NotificationServlet.doPost(NotificationServlet.java:44)
    at com.comviva.workflow.ui.notification.NotificationServlet.doGet(NotificationServlet.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    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.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Edit :-

I am not so good in these technologies, but as per my knowledge I am using these because-

Spring - To create bean objects in order to prevent multiple objects creation every time class access.

Servlet - I have to implement facebook like "notifications", so I am "clocking (Polling)" in JSP page the servlet URL to check whether any value updated or not.

JSF - I am using it for the creation of UI. I am very new in JSF, so I was not sure how to implement the Spring with it. So I asked whether I am missing something or not.

and I have setter/getter methods in my class. I just didn't show it to save the space. (apology if it confused the assumptions.)

Solution :-

I removed both beans within faces-config.xml and JSF annoations from Java class. I am catching the dao bean in java class itself by use of :-

WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
    notificationDao =  (NotificationDao)context.getBean("notificationDao");

Thanks for the suggestions.

theGamblerRises
  • 686
  • 1
  • 11
  • 27
  • Where exactly the exception is thrown, can you post the stack trace ? – Bren Aug 13 '13 at 15:41
  • By the way, you have defined ManagedBean and Managed property in faces-config xml along with annotations in class. One of them would suffice. They are two different ways to declare managed beans and properties. – Bren Aug 13 '13 at 15:44
  • I did the changes, please check. – theGamblerRises Aug 13 '13 at 15:49
  • 1
    Why does your managed bean need to extend also a servlet? Please provide the doGet/doPost method also. – Seitaridis Aug 13 '13 at 16:21
  • 1
    I strongly recommend to stop immediately and take a step back. You clearly missed the whole point of 1) [Servlets](http://stackoverflow.com/tags/servlets/info), 2) [JSF](http://stackoverflow.com/tags/jsf/info) and 3) [Spring](http://stackoverflow.com/questions/13011392/jsf-service-layer) and are somehow seeing all those APIs/frameworks as one and same thing and throwing them together in one pile. Essentially, your concrete problem is caused because you have 2 completely distinct instances of the same class, one acting as a JSF managed bean and another one acting as a HTTP servlet. – BalusC Aug 13 '13 at 16:25

2 Answers2

2

Well there are a bunch of problems here.

First of all, you can either declare beans within faces-config.xml or use JSF annoations to do it in the class code.

Also for a managed property to be set at run time you need setter and getter methods for that property. So you should change your java class like this:

Java Class

public class NotificationServlet extends HttpServlet
{

private static final long serialVersionUID = -3905661117247475401L;

//No need for annotations since you have managed-property definition in faces-config.xml whichever you prefer. 
//Also if you use the annoation the right way to do it would be @ManagedProperty("value =  #{notificationDao}"), you missed value = there

NotificationDao notificationDao;


public NotificationDao getNotificationDao() {
    return notificationDao;
}

public void setNotificationDao(NotificationDao notificationDao) { //Setter getter necessary
    this.notificationDao = notificationDao;
}


}

One last reminder. Make sure you put the below line into faces-config.xml between applcation tags, so that JSF will be able to use Spring bean definitions as managed beans

<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
Bren
  • 2,148
  • 1
  • 27
  • 45
  • See, I have setter/getters, I just didn't show them in post to save the space. I have el-resolver in the faces-config.xml. I removed both beans within faces-config.xml and JSF annoations from Java class. I am catching the dao bean in java class itself by use of WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); – theGamblerRises Aug 14 '13 at 17:54
0

I removed both beans within faces-config.xml and JSF annoations from Java class. I am catching the dao bean in java class itself by use of :-

WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
notificationDao =  (NotificationDao)context.getBean("notificationDao");

If it's another class, I am using this -

ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
notificationDao = (NotificationDao) ctx.getBean("notificationDao");
theGamblerRises
  • 686
  • 1
  • 11
  • 27