问题描述
我用SSH做了个java web项目,hibernate我是用注解的方式,spring我用xml来配置,首先我定义了一个user Model,以及它对应的userDao,userDaoImpl,userService,userServiceImpl,发现可以正确运行,然后我定义了和user完全一样的people Model,代码如下people:@Entitypublic class people {private int id;private String username;private String password; 省略set get方法}peopleDaoImple:public class peopleImpl implements peopleDao {private HibernateTemplate hibernateTemplate;public HibernateTemplate getHibernateTemplate() {return hibernateTemplate;}public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}public boolean save(people u) {hibernateTemplate.save(u);return true;}}peopleImpl:public class peopleImpl implements peopleService {private peopleDao peopledao;public peopleDao getPeopledao() {return peopledao;}public void setPeopledao(peopleDao peopledao) {this.peopledao = peopledao;}public boolean save(people u) {peopledao.save(u);return true;}}对应的applicationContext的配置如下:<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" > <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> <property name="annotatedClasses"> <list> <value>com.model.User</value> <value>com.model.people</value> </list> </property></bean><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" <property name="sessionFactory" ref="sessionFactory"></property></bean>peopleDaoImpl的配置如下: <bean id="peopledao" class="com.Dao.Impl.peopleImpl" > <property name="hibernateTemplate" ref="hibernateTemplate"></property></bean>peopleImpl配置如下:<bean id="peopleService" class="com.ServiceDao.Impl.peopleImpl" > <property name="peopledao" ref="peopledao"></property></bean>我觉得奇怪的是 我单单配置一个user时程序没问题,而多加了一个people以后则没办法用spring来为people注入,运行时tomcat服务器没有问题,但浏览器上出现错误500 然后报java.lang.NullPointerExceptioncom.ServiceDao.Impl.peopleImpl.save(peopleImpl.java:21)com.Actions.peopleAction.Register(peopleAction.java:44)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)java.lang.reflect.Method.invoke(Method.java:597)com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:163)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:249)org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:148)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:93)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:128)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:104)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:148)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:128)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:236)org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:468)org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76) 问题补充:lizhi92574 写道
解决方案
应该是peopleImpl peopledao属性没有注入成功是null。你执行peopledao.save(u); 出现的错误。
解决方案二:
你打断点查看 peopleImpl peopledao属性就知道是否有值了。
解决方案三:
应该是你注入的问题,看下你注入的包路径,类名什么的写对了没有
解决方案四:
引用public boolean save(people u) { hibernateTemplate.save(u); return true; } 1.0 people u 没传值2.0 也有可能 hibernateTemplate 没拿进来!
解决方案五:
看错误信息,应该是保存时,people对象应该没有初始化,为null,看看调用save方法的action,people是怎么来进行实例化的。
解决方案六:
这个不是注入报的错,是你调用了save()方法,传的参数为空对象,所以报空指针异常。