0

This is my Web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>loungeHotel</display-name>
  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
  </servlet>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/loungeHotel.xml
        </param-value>
    </context-param>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

This is my loungeHotel.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                    http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                    http://www.springframework.org/schema/tx 
                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd     
                    http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.1.xsd">


<bean id = "updateProfileDao"  class="com.lounge.dao.impl.UpdateProfileDaoImpl"></bean>

<bean id = "updateProfileFacade"  class="com.lounge.facades.impl.UserProfileFacadeImpl">
    <property name="updateProfileDao" ref="updateProfileDao"></property>
</bean>

</beans>    

this is the java file(UserProfileFacadeImpl.java) where i want to use my bean (updateProfileDao)

package com.lounge.facades.impl;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Required;

import com.lounge.dao.impl.UpdateProfileDaoImpl;
import com.lounge.dataModels.StudentData;
import com.lounge.facade.UserProfileFacade;

public class UserProfileFacadeImpl implements UserProfileFacade {

    @Resource(name="updateProfileDao")
    private UpdateProfileDaoImpl updateProfileDao;


    public UpdateProfileDaoImpl getUpdateProfileDao() {
        return updateProfileDao;
    }

    @Required
    public void setUpdateProfileDao(UpdateProfileDaoImpl updateProfileDao) {
        this.updateProfileDao = updateProfileDao;
    }
    @Override
    public StudentData updateProfile(StudentData studentData,String name){
        return getUpdateProfileDao().updateColumn(studentData, name);
    }
}

here is stacktrace...

>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

>java.lang.**NullPointerException**
    com.lounge.facades.impl.UserProfileFacadeImpl.updateProfile(UserProfileFacadeImpl.java:30)
    com.lounge.controllers.AccountPageController.updateProfile(AccountPageController.java:63)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
Zia
  • 1,001
  • 1
  • 13
  • 25
Amit mahala
  • 35
  • 2
  • 9
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jordi Castilla Jan 19 '16 at 09:54
  • He knows what an NPE is. What he doesn't know is why his bean does not get injected. – Adam Arold Jan 19 '16 at 09:59
  • i think its different , when i am creating a object with new keyword its working fine but when creating a bean in Xml file and then using that bean in my java file using @Required annotation then it's giving null pointer – Amit mahala Jan 19 '16 at 10:00
  • go to UserProfileFacadeImpl.java:30 – alex Jan 19 '16 at 10:19
  • Post the code for your controller class. – chrylis -cautiouslyoptimistic- Jan 19 '16 at 10:29
  • How did you access UserProfileFacadeImpl ? Is this Autowired or not? – Ruelos Joel Jan 19 '16 at 10:30
  • public static StudentData updateProfile( @ModelAttribute("student1") StudentData student1, final Model model, HttpSession session) { UserProfileFacade userProfileFacade = new UserProfileFacadeImpl(); StudentData student = (StudentData) session.getAttribute("student"); StudentData studentObj = userProfileFacade.updateProfile(student1 ,student.getName()); return studentObj ; } – Amit mahala Jan 19 '16 at 10:57
  • @fuzzy28 :- i am using new operator for that – Amit mahala Jan 19 '16 at 11:00
  • if you create an instance of UserProfileFacadeImpl using the new operator, you are explicitly not using spring to create your object. This means that spring is not managing the object you created, so none of the Spring annotations matter. Final result: no spring == no injection – DwB Jan 19 '16 at 15:53

4 Answers4

0

Here actually @Resource doesn't matter as you're explicitly setting the property in the xml.

I assume the root cause is rather in the controller, most probably the object is instantiated directly instead of being injected by application container.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
0

Since you are using SpringMVC I would recommend use @Autowired to load your Dao as service instead of using @Resource

Suvoraj Biswas
  • 573
  • 1
  • 6
  • 10
0

Based on your comment, you are accessing UserProfileFacadeImpl via new operator. This defeats the purpose of dependency injection and the reason why you are encountering a NPE. UserProfileFacadeImpl should be accessed by getting it in Spring container.

Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33
0

Add a bean of type UserProfileFacadeImpl to your spring configuration file (loungeHotel.xml).

Inject this bean instead of calling new.

DwB
  • 37,124
  • 11
  • 56
  • 82