首頁 > Java > java教程 > Java 報了個空指針,請大神看看問題何在?

Java 報了個空指針,請大神看看問題何在?

php是最好的语言
發布: 2018-08-04 11:24:41
原創
1359 人瀏覽過

<a href="javascript:void(0);" onclick="clearCart()" id="clear" class="clear">清空购物车</a>
登入後複製

//cart.jsp頁面上點選清空購物車事件

function clearCart(){
            if(confirm("您是否要清空购物车?")){
                location.href="${pageContext.request.contextPath }/product?method=clearCart";
            }
        }
登入後複製
登入後複製
public void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");        try {            //1.获得请求对象的method的名称
            String methodName = req.getParameter("methodName");            //2.获得当前被访问的对象的字节码对象
            Class clazz = this.getClass();//ProductServlet.class 或者

            //3.获得当前字节码对象中的指定方法
            Method method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);            //4执行相应功能方法
            method.invoke(this, req,resp);

            } catch (Exception e) {
                e.printStackTrace();
        }
    }
登入後複製
登入後複製

//清空購物車方法

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

        HttpSession session = request.getSession();
        session.removeAttribute("cart");        //跳转回cart.jsp
        response.sendRedirect(request.getContextPath()+"/cart.jsp");

    }
登入後複製
登入後複製
我点击清空就报异常了
登入後複製
登入後複製
java.lang.NullPointerException
    at java.lang.Class.searchMethods(Class.java:2832)
    at java.lang.Class.getMethod0(Class.java:2856)
    at java.lang.Class.getMethod(Class.java:1668)
    at com.itheima.web.servlet.BaseServlet.service(BaseServlet.java:25)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
登入後複製
登入後複製
<a href="javascript:void(0);" onclick="clearCart()" id="clear" class="clear">清空购物车</a>
登入後複製

//cart.jsp頁面上點擊清空購物車事件

function clearCart(){
            if(confirm("您是否要清空购物车?")){
                location.href="${pageContext.request.contextPath }/product?method=clearCart";
            }
        }
登入後複製
登入後複製
public void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");        try {            //1.获得请求对象的method的名称
            String methodName = req.getParameter("methodName");            //2.获得当前被访问的对象的字节码对象
            Class clazz = this.getClass();//ProductServlet.class 或者

            //3.获得当前字节码对象中的指定方法
            Method method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);            //4执行相应功能方法
            method.invoke(this, req,resp);

            } catch (Exception e) {
                e.printStackTrace();
        }
    }
登入後複製
登入後複製

//清空購物車方法

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

        HttpSession session = request.getSession();
        session.removeAttribute("cart");        //跳转回cart.jsp
        response.sendRedirect(request.getContextPath()+"/cart.jsp");

    }
登入後複製
登入後複製
我点击清空就报异常了
登入後複製
登入後複製
java.lang.NullPointerException
    at java.lang.Class.searchMethods(Class.java:2832)
    at java.lang.Class.getMethod0(Class.java:2856)
    at java.lang.Class.getMethod(Class.java:1668)
    at com.itheima.web.servlet.BaseServlet.service(BaseServlet.java:25)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
登入後複製
登入後複製

#相關文章:

大神幫忙看下這個空指標異常怎麼解決

在搭配php環境是遇到了一個棘手的相關問題,請各大神都幫忙看看!

以上是Java 報了個空指針,請大神看看問題何在?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板