获取空指针错误

时间:2015-06-15 11:56:53

标签: spring-mvc tiles jspdf

我正在使用spring mvc。 LoginService是界面。当我从控制器传递两个列表时,它工作正常。 当从控制器传递然后选择主页链接时,其他菜单都消失了。因为我正在使用Apache tile模板。这就是我尝试如下的原因。 但现在它正在获得空指针异常。

<% 
    LoginService loginService=null;    
    int userExists=1;

    request.setAttribute("MaintabsList",loginService.listMaintabs(userExists));
    request.setAttribute("SubtabsList", loginService.listSubtab(userExists));  
%> 
<nav id="bb" class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed"
                data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span> <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="Loginsucess">home</a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
           <ul class="nav navbar-nav">
                <c:if test="${not empty SubtabsList}">
                    <c:forEach var="maintab" items="${MaintabsList}">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown"
                               role="button" aria-expanded="false">${maintab.description}
                                <span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu" role="menu">
                                <c:forEach var="subtab" items="${SubtabsList}">
                                    <c:if test="${subtab.maintab.maiTabId == maintab.maiTabId}">
                                        <li>
                                            <a href="${subtab.ref}">${subtab.description}</a>
                                        </li>
                                    </c:if>
                                </c:forEach>
                            </ul>
                        </li>
                    </c:forEach>
                </c:if>
            </ul>
        </div>
    </div>
</nav>
</body>
</html>

我收到的错误如下:

java.lang.NullPointerException
    at org.apache.jsp.WEB_002dINF.jsp.menu_jsp._jspService(menu_jsp.java:110)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:605)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:544)
    at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
    at org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
    at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
    at org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:103)
    at org.apache.tiles.jsp.context.JspTilesRequestContext.dispatch(JspTilesRequestContext.java:96)
    at org.apache.tiles.renderer.impl.TemplateAttributeRenderer.write(TemplateAttributeRenderer.java:44)
    at org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
    at org.apache.tiles.renderer.impl.ChainedDelegateAttributeRenderer.write(ChainedDelegateAttributeRenderer.java:76)
    at org.apache.tiles.renderer.impl.AbstractBaseAttributeRenderer.render(AbstractBaseAttributeRenderer.java:106)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:670)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:336)
    at org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:210)
    at org.apache.tiles.template.InsertAttributeModel.end(InsertAttributeModel.java:126)
    at org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:311)
    at org.apache.jsp.WEB_002dINF.jsp.main_jsp._jspx_meth_tiles_005finsertAttribute_005f2(main_jsp.java:149)
    at org.apache.jsp.WEB_002dINF.js

1 个答案:

答案 0 :(得分:0)

您尚未初始化loginService对象,这是您在调用方法NullPointerException时获得loginService.listMaintabs(userExists)的原因。

你必须用它的实现类初始化loginService对象,如下所示:

<% 
    LoginService loginService = new LoginServiceImpl();
    System.out.print("shaaaaa");
    int userExists=1;

    request.setAttribute("MaintabsList", loginService.listMaintabs(userExists));
    request.setAttribute("SubtabsList", loginService.listSubtab(1));  
    System.out.print("shaaaaa2");
%>