Rumah > Java > javaTutorial > teks badan

Java 报了个空指针,请大神看看问题何在?

php是最好的语言
Lepaskan: 2018-08-04 11:24:41
asal
1330 orang telah melayarinya

<a href="javascript:void(0);" onclick="clearCart()" id="clear" class="clear">清空购物车</a>
Salin selepas log masuk

//cart.jsp页面上点击清空购物车事件

function clearCart(){
            if(confirm("您是否要清空购物车?")){
                location.href="${pageContext.request.contextPath }/product?method=clearCart";
            }
        }
Salin selepas log masuk
Salin selepas log masuk
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();
        }
    }
Salin selepas log masuk
Salin selepas log masuk

//清空购物车方法

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

    }
Salin selepas log masuk
Salin selepas log masuk
我点击清空就报异常了
Salin selepas log masuk
Salin selepas log masuk
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)
Salin selepas log masuk
Salin selepas log masuk
<a href="javascript:void(0);" onclick="clearCart()" id="clear" class="clear">清空购物车</a>
Salin selepas log masuk

//cart.jsp页面上点击清空购物车事件

function clearCart(){
            if(confirm("您是否要清空购物车?")){
                location.href="${pageContext.request.contextPath }/product?method=clearCart";
            }
        }
Salin selepas log masuk
Salin selepas log masuk
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();
        }
    }
Salin selepas log masuk
Salin selepas log masuk

//清空购物车方法

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

    }
Salin selepas log masuk
Salin selepas log masuk
我点击清空就报异常了
Salin selepas log masuk
Salin selepas log masuk
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)
Salin selepas log masuk
Salin selepas log masuk

相关文章:

大神帮忙看下这个 空指针异常 怎么解决

在搭配php环境是遇到了一个棘手的有关问题,请各大神都帮忙看看!

Atas ialah kandungan terperinci Java 报了个空指针,请大神看看问题何在?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan