为什么JSP调用静态方法就出错

类型例外报告

消息 在第21行处理JSP页面/add.jsp时发生异常

描述 服务器遇到内部错误,导致服务器无法满足此请求。

例外

org.apache.jasper.JasperException:在第21行处理JSP页面/add.jsp时发生异常

18:<%
19:字符串productId = request.getParameter(“ productId”);
20:ProductQuery pro = new ProductQuery();
21:products.add(pro.queryProductById(productId));
22:%>
23:</ body>
24:</ html>


堆栈跟踪:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

根本原因

java.lang.NullPointerException
	org.apache.jsp.add_jsp._jspService(add_jsp.java:135)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note注意 在Apache Tomcat / 8.5.9日志中可以找到根本原因的完整堆栈跟踪

//Product类就不复制了,里面包含get,set函数还有3个私有属性。。

public class ProductQuery {
    public static Product queryProductById(String id) {
        Product product = null;
        if (id.equals("1")) {
            product = new Product(id, "iPhone", 6000.0);
        }

------------------------------------------------------------------------------------------------------------------

<%
String productId =request.getParameter("productId");
ProductQuery pro = new ProductQuery();
products.add(pro.queryProductById(productId));
%>

然后是index.jsp部分代码

 

<%@ page pageEncoding="UTF-8" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page import="test7.Product" %>

<%
  List<Product> products = (List<Product>)session.getAttribute("products");
  if (products == null) {
    products = new ArrayList<Product>();
    session.setAttribute("products", products);
  }
%>

查看全部
weixin_46098228
weixin_46098228
2020/11/25 12:51
  • java
  • jar
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信

2个回复