问题描述
[color=#FF0000]java.lang.NullPointerExceptioncom.hq.ictbox.struts.action.UserAction.execute(UserAction.java:53)sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)java.lang.reflect.Method.invoke(Method.java:597)[/color]下面是Action:packagecom.hq.ictbox.struts.action;importcom.hq.ictbox.hi.dao.TbBoxDAO;importcom.hq.ictbox.hi.dao.TbUserDAO;importcom.opensymphony.xwork2.ActionSupport;publicclassUserActionextendsActionSupport{privatestaticStringFORWARD=null;//Action类公用私有变量,用来做页面导航标志privateStringuserName;privateStringuserPassword;privateTbUserDAOtbUserDAO;privateTbBoxDAOtbBoxDAO;//依赖注入业务逻辑组件的setter方法publicvoidsetTbUserDAO(TbUserDAOtbUserDAO){this.tbUserDAO=tbUserDAO;}//依赖注入业务逻辑组件的setter方法publicvoidsetTbBoxDAO(TbBoxDAOtbBoxDAO){this.tbBoxDAO=tbBoxDAO;}publicStringgetUserName(){returnuserName;}publicvoidsetUserName(StringuserName){this.userName=userName;}publicStringgetUserPassword(){returnuserPassword;}publicvoidsetUserPassword(StringuserPassword){this.userPassword=userPassword;}publicStringexecute()throwsException{userName=getUserName().trim();//属性值即JSP页面上输入的值userPassword=getUserPassword().trim();//属性值即JSP页面上输入的值System.out.print(userName);System.out.print(userPassword);tbUserDAO.print(userName,userPassword;)这行的print()只是一个简单的打印方法.........................................53行try{FORWARD="success";}catch(Exceptionex){ex.printStackTrace();}returnFORWARD;}}配置文件:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"value="com.mysql.jdbc.Driver"/><propertyname="url"value="jdbc:mysql://localhost:3306/db_ictboxm"/><propertyname="username"value="root"/><propertyname="password"value="caiqi"/></bean><!--Configurationhibernate--><beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="mappingResources"><list><value>com/hq/ictbox/hi/mapping/TbUser.hbm.xml</value><value>com/hq/ictbox/hi/mapping/TbBox.hbm.xml</value><value>com/hq/ictbox/hi/mapping/TbExt.hbm.xml</value></list></property><propertyname="hibernateProperties"><props><propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><propkey="show_sql">true</prop><propkey="hibernate.hbm2ddl.auto">update</prop><propkey="hibernate.jdbc.batch_size">20</prop></props></property></bean><beanid="TbUserDAO"class="com.hq.ictbox.hi.dao.TbUserDAO"><propertyname="sessionFactory"ref="sessionFactory"/></bean><beanid="TbBoxDAO"class="com.hq.ictbox.hi.dao.TbBoxDAO"><propertyname="sessionFactory"ref="sessionFactory"/></bean><beanid="TbExtDAO"class="com.hq.ictbox.hi.dao.TbExtDAO"><propertyname="sessionFactory"ref="sessionFactory"/></bean><!--Configurationtransaction--><beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><beanid="transactionInterceptor"class="org.springframework.transaction.interceptor.TransactionInterceptor"><propertyname="transactionManager"ref="transactionManager"/><propertyname="transactionAttributes"><props><propkey="get*">PROPAGATION_REQUIRED,readOnly</prop><propkey="*">PROPAGATION_REQUIRED</prop></props></property></bean><beanid="BeanNameAutoProxyCreator"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><propertyname="beanNames"><list><value>IUserDao</value></list></property><propertyname="interceptorNames"><list><value>transactionInterceptor</value></list></property></bean></beans>
解决方案
解决方案二:
问题我自己还是找出来了,<beanid="TbUserDAO"class="com.hq.ictbox.hi.dao.TbUserDAO"><propertyname="sessionFactory"ref="sessionFactory"/></bean>中的id="TbUserDAO"应该对应Action中定义的tbUserDAO
解决方案三:
有时,应该靠自己,耐住性子认真的弄懂问题