请各位大神帮我看看,struts2做的小项目,action中调用接口方法,显示nullpoint异常,可是单元测试方法没有问题啊
这是接口public inte易做图ce ProvinceService {
public boolean addProvince(Province province);
public boolean delProvince(Province province);
public List<Province> findAll();
public boolean updateProvince(Province province);
public List<Course> querryCourseByPro(Province province);
实现方法
public List<Province> findAll() {
List<Province> provinces = new ArrayList<Province>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Province province = null;
conn = DB.getConn();
try {
ps = conn.prepareStatement("select * from province");
rs = ps.executeQuery();
while (rs.next()) {
province = new Province();
province.setId(rs.getString("id"));
province.setName(rs.getString("name"));
provinces.add(province);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return provinces;
}
action :::
import java.util.List;
import java.util.Map;
import yang.bai.bean.Province;
import yang.bai.service.ProvinceService;
import yang.bai.service.impl.ProvinceServiceBean;
import com.opensymphony.xwork2.ActionSupport;
public class ProvinceAction extends ActionSupport {
private Map<String, Object> session;
public Map<String, Object> getSession() {
return session;
}
public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}
public String getAll() {
ProvinceService provinceService = new ProvinceServiceBean();
List<Province> provinces = provinceService.findAll();
session.put("pro", provinces);
return "showPro";
}
}
结果就是显示
ype Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
yang.bai.action.ProvinceAction.getAll(ProvinceAction.java:53)
sun.reflect.GeneratedMethodAccessor40.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
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:249)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
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:249)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
求大神救救小弟,研究好几天了 action strut2 空指针 接口调用 --------------------编程问答-------------------- province = new Province();放到while外部。 --------------------编程问答-------------------- 出错的是哪一行? --------------------编程问答-------------------- 初步肯定是dao中findAll()的问题,
PreparedStatement ps = null;//你这里定义为null
rs = ps.executeQuery();//下面又使用它的方法,这样应该会报空指针吧 --------------------编程问答--------------------
不好意思,又看错了,好像没什么问题,你可以编一个测试类测试一下dao中的findAll()方法 --------------------编程问答--------------------
没有行数,就是getALL方法、 --------------------编程问答-------------------- 问题应该在这里ProvinceService provinceService = new ProvinceServiceBean();
你dao中的方法是没有错,你System.out.println(provinceService==null)一下;如果是true,那么恭喜你,问题就是你没有拿到provinceService,你自己看看吧。如果是false,那就不知道了
--------------------编程问答-------------------- 楼上说了一点:ProvinceService provinceService = new ProvinceServiceBean();
你dao中的方法是没有错,你System.out.println(provinceService==null)一下;如果是true,那么恭喜你,问题就是你没有拿到provinceService,你自己看看吧。如果是false,那就不知道了
我再补充一点,如果是flase,你就到findAll()方法中设个断点调试一下 --------------------编程问答--------------------
不是,dao里的方法好使,就是放到action就不好使了,是struts2 的action问题, --------------------编程问答--------------------
不是,dao里的方法好使,就是放到action就不好使了,是struts2 的action问题, --------------------编程问答-------------------- provinceService==null 这个是true还是false?? --------------------编程问答-------------------- 楼主试试吧实体类对象创建和赋值都放在循环里
Province province=new Province();
补充:Java , Java EE