Spring @Resource空指针异常

时间:2018-01-08 14:42:33

标签: java spring jsp spring-mvc

我正在创建一个Spring MVC应用程序。我有一个篮子豆,里面装的豆子很少。

@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;
    }

自动装配的bean CPContainer和FBContainer的内部列表通过@Resource注释连接到dispather-servltet.xml中的列表:

@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类添加到模型作为模型属性,我打算在jsp页面中使用从Container的列表中获取数据(如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 个答案:

答案 0 :(得分:0)

我想我找到了解决方案。看来我在MainController中的计算方法模型中添加了错误的对象:

mapply(c, out, mapply(rep, NA, max(nout)-nout))

所以,我从方法中删除了Basket参数,并解决了NullPointerException:

@RequestMapping(value = "/calculate",method = RequestMethod.GET)
    public String calculate(Model model,Basket basket){
        model.addAttribute("Basket",basket);
        return "calculate";
    }