getting null pointer error

215 views Asked by At

I am using spring mvc. LoginService is interface. When I pass two lists from controller it's working fine. When passing from controller and then selecting the home page link other menus are disappeared.because i'm using Apache tile template. That's why I tried as below. But now it's getting null pointer exception.

<% 
    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>

I am getting the error as follows :

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

There are 1 answers

5
vivekpansara On

You haven't initialize the loginService object that's why you are getting NullPointerException when it calls the method loginService.listMaintabs(userExists).

You have to initialize the loginService object with it's implementation class like below:

<% 
    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");
%>