问题描述
异常信息如下:description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerExceptioncom.axisoft.applicationExercise.action.testAction.testMethod(testAction.java:25)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)com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)Web.XML信息如下:<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext*.xml</param-value></context-param><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><session-config><session-timeout>30</session-timeout></session-config><listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>applicationContext.xml信息如下:<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/><property name="url" value="jdbc:oracle:thin:@xxx.xxx.x.xxx:1521:PEGA"/>(IP用"x"代替)<property name="username" value="RAIN"/><property name="password" value="RAIN"/></bean><!-- Hibernate SessionFactory --><bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.jdbc.fetch_size">50</prop><prop key="hibernate.jdbc.batch_size">50</prop></props></property></bean><!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) --><bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><!-- Transaction manager for a single Jdbc DataSource --><bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"><property name="transactionManager" ref="transactionManager" /></bean> <!-- Jdbc template for a single Jdbc DataSource --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="testService" class="com.axisoft.applicationExercise.createApplication.service.TestService" />Action信息如下:private ITestService testService;public String testMethod(){/*if (null == testService){System.out.println("果为空");return SUCCESS;}*/String helloStr = testService.testMethod();System.out.println("helloStr : " + helloStr);System.out.println("测试成功 : " + helloStr);return SUCCESS;}public ITestService getTestService(){return testService;}public void setTestService(ITestService testService){this.testService = testService;}Service信息如下:@Overridepublic String testMethod(){//testDao.testMethod();return "Hello";}JSP页面信息如下<a href="testSS.action"><click me></a><form action="testSS.action"><input type="submit" value="ddd"/></form>哪位请指点一下,错误在哪,当我点击链接或者按钮的时候为什么会出现NullPointerException,谢谢了 问题补充:问题的根源,就是testService没有注入可是为什么呢,struts2的注入很简单的,只用写getter和setter方法,或者用注解可以实现,但是好像要导入spring-struts-3.0.2.RELEASE.jar这个包,请各位再看看我的问题出在哪里,我的所有配置信息已经列出来了。
解决方案
首先看你的配置文件struts没有整合spring。Action testService属性没有使用spring注入,是个null;struts.xml 加入该属性 还需要相应版本的struts2-spring-plugin.jar包<!-- 该属性指定Struts 2中的action由Spring容器创建 --><constant name="struts.objectFactory" value="spring" />然后配置spring action bean 并注入testService
解决方案二:
引用struts2的注入很简单的,只用写getter和setter方法,或者用注解可以实现,但是好像要导入spring-struts-3.0.2.RELEASE.jar这个包,请各位再看看我的问题出在哪里,我的所有配置信息已经列出来了。 问题不是很明确了嘛!只用写getter和setter方法 ???不是吧! spring action bean 并注入testService 这样才能注入 或者 annotation注入!
解决方案三:
testService 应该没有注入实例。可以通过debug启动webserver,程序加上断点跟一下,就知道原因了。
解决方案四:
引用com.axisoft.applicationExercise.action.testAction.testMethod(testAction.java:25) testAction 的第25 行!应该是这行吧引用String helloStr = testService.testMethod(); 貌似 private ITestService testService; 没注入进去 呵呵! 换成 public 或者检查下其他配置