我正在创建一个SpringMVC应用程序,并且我有一个Basketbean,其中包含一些autowired bean。
@Component
public class Basket {
@Autowired
@Qualifier("CPContainer")
private CPContainer cpContainer;
@Autowired
@Qualifier("FBContainer")
private FBContainer fbContainer;
public CPContainer getCpContainer() {
return cpContainer;
}
public void setCpContainer(CPContainer cpContainer) {
this.cpContainer = cpContainer;
}
public FBContainer getFbContainer() {
return fbContainer;
}
public void setFbContainer(FBContainer fbContainer) {
this.fbContainer = fbContainer;
}
Autowired beans CPContainer和FBContainer内部有List,通过@Resource annotation与dispather-servltet.xml中的list连接:
@Component
public class FBContainer implements Container {
@Resource(name = "FBList")
private List<FBProduct> products;
private int id;
private int amount;
@Override
public List<FBProduct> getProducts() {
return products;
}
@Override
public FBProduct getProductById(int id) {
return products.get(id);
}
dispatcher-servltet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans ...
<mvc:annotation-driven/>
<context:component-scan base-package="com.concrete"/>
<util:list id="FBList" value-type="com.concrete.pojo.product.FBProduct">
<bean id="fb1" class="com.concrete.pojo.product.FBProduct">
<property name="id" value="0"/>
<property name="mass" value="1000"/>
<property name="l" value="8980"/>
<property name="b" value="1490"/>
<property name="h" value="220"/>
</bean>
<bean id="fb2" class="com.concrete.pojo.product.FBProduct">
<property name="id" value="1"/>
<property name="mass" value="2000"/>
<property name="l" value="8980"/>
<property name="b" value="1190"/>
<property name="h" value="220"/>
</bean>
<bean id="fb3" class="com.concrete.pojo.product.FBProduct">
<property name="id" value="2"/>
<property name="mass" value="3000"/>
<property name="l" value="7180"/>
<property name="b" value="1490"/>
<property name="h" value="220"/>
</bean>
</util:list>
<bean id="FBContainer" class="com.concrete.pojo.container.FBContainer">
<property name="products" ref="FBList"/>
</bean>
<bean id="CPContainer" class="com.concrete.pojo.container.CPContainer">
<property name="products" ref="CPList"/>
</bean>
</beans>
我将Basket类作为model属性添加到模型中,我打算在jsp页面中使用它来从Container的List中获取数据(就像FBContainer中的FBProducts一样)。但是当我试图访问List时,我得到空指针异常:
java.lang.NullPointerException
com.concrete.pojo.container.FBContainer.getProductById(FBContainer.java:25)
com.concrete.pojo.container.Basket.calculateMass(Basket.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
javax.el.BeanELResolver.invoke(BeanELResolver.java:183)
org.apache.jasper.el.JasperELResolver.invoke(JasperELResolver.java:139)
org.apache.el.parser.AstValue.getValue(AstValue.java:174)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:950)
org.apache.jsp.calculate_jsp._jspService(calculate_jsp.java:93)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:170)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1325)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1069)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1008)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
MainController.class:
@Controller
public class MainController {
@Autowired
@Qualifier("basket")
private Basket basket;
@RequestMapping(value = "/calculate",method = RequestMethod.POST)
public String calculate(Model model,Basket basket){
model.addAttribute("Basket",basket);
return "calculate";
}
}
1条答案
按热度按时间vltsax251#
我想我找到了解决方案。看起来我在MainController内的计算方法模型中添加了错误的对象:
因此,我从方法中删除了Basket参数,并解决了NullPointerException: