Servlet view.forward(request,response)方法中的空指针异常

时间:2014-08-22 12:27:41

标签: servlets

文件main.jsp中的Javascript

    <script>
        var form = null;
        var item = null;
        window.onload = init;
        function init(){
            form = document.getElementsByTagName('form')[0];                    
            item = document.getElementById("selectedItem"); 

        }
        function handleCellClick(e)
        {
            item.value = e.innerHTML;
            alert(item.value);
            form.submit();
        }
    </script>

和main.jsp文件包含

<body>
        <ul id="menuList">
            <li class="mainMenuItem">Master</li>
            <li class="subMenuItem clickCell" onClick="handleCellClick(this)">Cashier Info.</li>
            <li class="subMenuItem">Product Info.</li>
            <li class="subMenuItem">Price list Info.</li>
            <li class="subMenuItem">Agent Info</li>
        <li class="mainMenuItem">Transaction</li>
            <li class="subMenuItem">Invoice Creation</li>
            <li class="subMenuItem">Invoice Correction</li>
            <li class="subMenuItem">Change Invoice Date</li>
        <li class="mainMenuItem">Report</li>
            <li class="subMenuItem">Daily Sale Report</li>
            <li class="subMenuItem">Trucksheet</li>
            <li class="subMenuItem">Agent Sale Report</li>
        <li class="mainMenuItem">Logoff</li>
    </ul>
    <form action="<c:url value='Main.do' />">
        <input id="selectedItem" name="selectedItem" type="hidden" />
    </form>
</body>

和Servlet包含

public class MainProgram extends HttpServlet {

    public void doGet (HttpServletRequest request, HttpServletResponse response) 
        throws IOException, ServletException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String jsp =null;
    String selectedItem = request.getParameter("selectedItem");



    HttpSession session = request.getSession(false);
    if(session==null){
        jsp = "login.jsp";
        RequestDispatcher view = request.getRequestDispatcher(jsp);
        view.forward(request, response);
    }else{
        jsp = "user_info.jsp";
        RequestDispatcher view = request.getRequestDispatcher(jsp);
        view.forward(request, response);     // this line gives NullPointerException
    }
}

public void doPost (HttpServletRequest request, HttpServletResponse response) 
        throws IOException, ServletException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String userName = request.getParameter("txtUserName");  
        String jsp = null;                      //clickme page

        if(userName.equals("Admin") || userName.equals("Operator") || userName.equals("Guest")){
            HttpSession session = request.getSession();
            session.setAttribute("regUser", userName);
            jsp = "main.jsp";
        }else{
            jsp = "login.jsp";      
        }
        RequestDispatcher view = request.getRequestDispatcher(jsp);
        view.forward(request, response);
    }
}

登录并提交后,转到main.jsp 当我点击收银台信息时,它会提醒执行servlet 但是servlet在最后一行给出了NullPointerException 那行包含

            view.forward(request, response);

HTTP状态500 - java.lang.NullPointerException .... .... com.program.web.MainProgram.doGet(MainProgram.java:31)

我现在停下来思考 我不明白为什么会这样呢

请指导!!!!

1 个答案:

答案 0 :(得分:0)

您已发布到.../Main.do,并尝试在main.jsp处调度servlet。服务器在.../Main.do/main.jsp服从并查找servlet: - (

我认为它什么也没找到,view是空的......

相对网址是一个持续的危险,可以使用,但小心

(但+1直接提供所有相关信息......)