第31行的JSP错误

时间:2016-06-01 19:07:29

标签: java sql apache jsp

当我运行jsp页面时,我将此错误,任何建议以解决它,(我知道jsp与java不太好)...

  

输入例外报告

     

message处理JSP页面时发生异常   第31行的/assets/jsp/create-dest-code.jsp

     

description服务器遇到阻止它的内部错误   完成此请求。

     

异常org.apache.jasper.JasperException:发生异常   在第31行处理JSP页面/assets/jsp/create-dest-code.jsp

     

28:if(id> -1)29:{30:int insert_ch; 31:for(int   I = 0;我   insert_ch = myStatement.executeUpdate(“INSERT INTO Dest_has_Categories   (Dest_idDest,Categories_idCategories)价值观   ('“+ id +”','“+ cat [i] +”')“); 34:}

     

堆栈跟踪:     org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)     javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

     

root cause java.lang.NullPointerException     org.apache.jsp.assets.jsp.create_002ddest_002dcode_jsp._jspService(create_002ddest_002dcode_jsp.java:136)     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:438)     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)     javax.servlet.http.HttpServlet.service(HttpServlet.java:729)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

     

注意Apache中提供了根本原因的完整堆栈跟踪   Tomcat / 8。0。3。3日志。

我的代码:

<%
String id8=request.getParameter("id8"); //Country field  
String id9=request.getParameter("id9"); //City field
String id10=request.getParameter("id10"); //URL field
int id = -1;

Class.forName("com.mysql.jdbc.Driver"); 
String myDatabase = "jdbc:mysql://localhost:3306/project_app?user=root&password=1234"; 
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();  

String sqlInsert = "INSERT INTO dest(Country,City,URL) VALUES ('"+id8+"', '"+id9+"','"+id10+"')";
myStatement.executeUpdate(sqlInsert);
ResultSet rs = myStatement.executeQuery("SELECT idDest FROM dest WHERE Country='"+id8+"' AND City='"+id9+"' AND URL='"+id10+"'" );
while (rs.next()) {
    id=rs.getInt(1);
}
String cat[]=request.getParameterValues("dest1");

if(id > -1)
{
    int insert_ch;
    for(int i=0;i<cat.length;i++)
    {
      insert_ch=myStatement.executeUpdate("INSERT INTO Dest_has_Categories (Dest_idDest,Categories_idCategories) VALUES ('"+id+"','"+cat[i]+"')");
    }
}


 myStatement.close(); 
   myConnection.close(); %>

   <h1 style="color:blue;">Successful Registration </h1>

2 个答案:

答案 0 :(得分:1)

我认为,一旦你知道第31行是什么,你就可以通过这个类型的许多其他问题修复你的NPE了,所以让我们关注如何在你的案例中找到#31行

根据您的堆栈跟踪,您使用的是Tomcat 8,因此您的jsp文件/assets/jsp/create-dest-code.jsp的源代码位于$TOMCAT_HOME/work/Catalina/localhost/assets/org/apache/jsp/jsp/create-dest-code_jsp.java,从这里检查第31行,您将知道如何解决它。

实际上你已经有了答案:

  

28:if(id> -1)29:{30:int insert_ch; 31:for(int i = 0; i   insert_ch = myStatement.executeUpdate(&#34; INSERT INTO Dest_has_Categories   (Dest_idDest,Categories_idCategories)价值观   (&#39;&#34 + ID +&#34;&#39;&#39;&#34 +猫[I] +&#34;&#39;)&#34); 34:}

您的变量cat似乎是null,这可能意味着您没有参数dest1的值。

答案 1 :(得分:0)

您确定连接字符串有效吗?在此处的示例中,代码中缺少连接字符串中的更多详细信息(如数据库名称):

Connection con = null;
    try {

      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost\\SQLEXPRESS;"
        + "user=sa;password=HerongY@ng;"
        + "database=AdventureWorks2014");

source