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注入报错
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注入报错
<bean id="u" class="com.njx.action.UserAction">
<action name="XXX" class="u">
2.debug看看,确认异常是action中抛出的还是service中抛出的。