spring ioc 注入,报空指针异常,求挑错。

热带鱼2020 2011-02-12 09:54:39
spring ioc 注入,messageService 报空指针异常,而userService 注入是正常的。


<bean id="messageDao" class="com.ssh2.dao.impl.MessageDaoImpl" scope="singleton">
<!-- 由于该类是继承于HibernateDaoSupport,所以需要将sessionFactory注入 -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" class="com.ssh2.dao.impl.UserDaoImpl" scope="singleton">
<!-- 由于该类是继承于HibernateDaoSupport,所以需要将sessionFactory注入 -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="userService" class="com.ssh2.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="messageService" class="com.ssh2.service.impl.MessageServiceImpl">
<property name="messageDao" ref="messageDao"/>
</bean>

<bean id="userAction" class="com.ssh2.action.user.UserAction" scope="prototype">
<property name="userService" ref="userService"/>
<property name="messageService" ref="messageService"/>
</bean>



public class UserAction extends ActionSupport {
private UserService userService;
private MessageService messageService;

public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
System.out.println( "user");
this.userService = userService;
}
public MessageService getMessageService() {
return messageService;
}
public void setMessageService(MessageService messageService) {
System.out.println( "message");
this.messageService = messageService;
}


public class MessageServiceImpl implements MessageService {
private MessageDao messageDao;

public MessageDao getMessageDao() {
return messageDao;
}
public void setMessageDao(MessageDao messageDao) {
this.messageDao = messageDao;
}


...全文
467 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
sunpengju0204 2012-03-03
我也只这个问题,等待解决呢
回复
zhangzhen6960 2012-01-04
兄弟 你这个问题解决没?我也遇到了 无语啊,不知道到底哪错了?
回复
sebatinsky 2011-02-12

<bean id="userAction" class="com.ssh2.action.user.UserAction" scope="prototype">
<property name="userService" ref="userService"/>
<property name="messageService" ref="messageService"/>
</bean>

楼主可以把这句去掉试试,因为你已经在spring当中配置了,其实不用写注入action的。当然,也许是我知识欠少,你可以根据具体的问题去看看,写一个方法,调用一次试试。看时候有问题。或者查看你的路径啊,还有其他什么相关的东西,是不是缺少了什么。
回复
a312983516 2011-02-12
错误发出来
回复
hzzduoduo 2011-02-12
出错信息发来
回复
爱吃面条 2011-02-12
把具体的出错日志发出来
回复
licip 2011-02-12
可能messageService本身创建不了,你先测试一下messageService能不能创建
回复
热带鱼2020 2011-02-12
messageService 报空指针异常,而userService 注入是正常的。
回复
xihuanni0509 2011-02-12
我觉得你这代码没什么问题
回复
热带鱼2020 2011-02-12
报错如下


java.lang.NullPointerException
com.ssh2.action.user.UserAction.postMessage(UserAction.java:211)
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:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
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:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
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:237)
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:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
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:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.TimerInterceptor.invokeUnderTiming(TimerInterceptor.java:124)
com.opensymphony.xwork2.interceptor.TimerInterceptor.intercept(TimerInterceptor.java:111)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.ssh2.action.user.AuthorityInterceptor.intercept(AuthorityInterceptor.java:16)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
com.ssh2.action.user.LoginFilter.doFilter(LoginFilter.java:42)


public String postMessage() throws Exception {
String userName = (String)ActionContext.getContext().getSession().get("userName");
User u = userService.findByName(userName);
msg.setUser(u);
msg.setDate(new Timestamp( System.currentTimeMillis()));
msg.setStatus(1);
System.out.println( msg );
System.out.println( messageService); //这句打印是null

messageService.addMessage(msg); // 这里是第211行
return SUCCESS;
}
回复
pl3121605999 2011-02-12
异常信息发出来
回复
klising_123 2011-02-12
大哥、真F了、你都说空指针异常了、光贴基础代码、报贴出错误来、还根据错误去找谁空指针了、。。。大海里让我们去捞针啊。。
回复
fendou5016 2011-02-12
你需要把你action里面的东西拿出来看看 空指针主要是由于 你注入不成功 没有产生了对象 然后又调用了该对象的方法 而造成的 你看action中 调用了哪个对象的方法出错的 就是哪个对象有问题!! 然后往回找---倒推
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2011-02-12 09:54
社区公告
暂无公告