java.lang.NullPointerException
com.njx.action.UserAction.execute(UserAction.java:17)
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)
web.xml:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringTest</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  </context-param>
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>applicationContext.xml:
<?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:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  <bean id="userDao" class="com.njx.dao.impl.UserDaoImpl"></bean>
  
  <bean id="userService" class="com.njx.service.impl.UserServiceImpl">
   <property name="userDao" ref="userDao" />
  </bean>  <bean id="u" class="com.njx.action.UserAction">
   <property name="userService" ref="userService" />
  </bean>  </beans>
UserAction.java:
package com.njx.action;import com.njx.service.UserService;
import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport{ /**
 * 
 */
private static final long serialVersionUID = 1L;

private UserService userService;

@Override
public String execute() throws Exception {
boolean b = userService.user_login();
return SUCCESS;
}

public UserService getUserService() {
System.out.println("userServic:" + userService);
return userService;
} public void setUserService(UserService userService) {
this.userService = userService;
}}
Spring注入报错

解决方案 »

  1.   

    代码里有加载applicationContext.xml么?
      

  2.   

    恩,大哥,你的struts.xml文件呢,你在里面配置Action了么,根据你的提示,是你的前台页面提交请求后,后台找不到Action中的方法。还有,如果你是spring来管理struts的Action的话,在struts.xml中配置action时,class属性里的类应该是spring中的bean id,在spring中配置action 的bean,希望对你有用!
      

  3.   

    1.struts.xml中一定要用spring中定义的
    <bean id="u" class="com.njx.action.UserAction">
    <action name="XXX" class="u">
    2.debug看看,确认异常是action中抛出的还是service中抛出的。
      

  4.   

    这两个配置没看出啥问题,自己看看自己的struts.xml文件,跟启动的时候日志,有没有正确加载这几个配置文件。
      

  5.   

    贴出Action  我给你看看