【问题标题】:Stacktrace:] with root cause java.lang.NullPointerExceptionStacktrace:] 根本原因是 java.lang.NullPointerException
【发布时间】:2016-01-13 11:21:06
【问题描述】:

此时代码出现错误

org.apache.catalina.core.StandardWrapperValve invoke SEVERE: 

Servlet.service() for servlet [jsp] in context with path [/Work] threw exception [An exception occurred processing JSP page /Display.jsp at line 49 
46:<td> 
47: <% 
48: String sql="select * from Patient "; 
49: pstmt=con.prepareStatement(sql); 
50: rs=pstmt.executeQuery(); 
51: while(rs.next()){ 
52: %> 

Stacktrace:] 根本原因是 java.lang.NullPointerException

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> 
<%@page import="java.sql.DriverManager"%> 
<%@page import="java.sql.ResultSet"%> 
<%@page import="java.sql.PreparedStatement"%>
 <%@page import="java.sql.Connection"%> 
<%@page import="com.connection.*"%> <body> 
<%! Connection con=null; 
PreparedStatement pstmt=null; 
ResultSet rs=null; 
public void init() { 
 try{ 
  Connection con = ConnectionImplementation.wantConnection();
    // Statement stmt= con.createStatement();
  } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
  } 
} 
%> 
<table> 
<tr> <td>
 <select name="PERSON_TYPE" id="PERSON_TYPE"> 
<option value="">SELECT</option> 
<option value="Doctor">Doctor</option>
 <option value="Nurse">Nurse</option> 
<option value="Patient">Patient</option>
 </select> </td> 
</tr> 
<tr> <td> 
<% String sql="select * from Patient "; 
pstmt=con.prepareStatement(sql); 
rs=pstmt.executeQuery();
 while(rs.next()){ %> 
<tr><td> <%= rs.getInt(1)%></td> 
<td><%= rs.getString(2) %></td> 
<td><%= rs.getString(3) %></td> 
<td><%= rs.getInt(4) %></td> 
<td><%= rs.getString(5) %></td> 
<td><%= rs.getString(6) %></td> 
<td><%= rs.getString(7) %></td></tr> 
<% } %> </table> </body> </html>

【问题讨论】:

标签: jsp pointers jdbc null


【解决方案1】:

您的成员变量con 从未被赋值,因此null

在您的 init() 方法中,您声明了一个 local 变量 con - 因此 成员变量 Connection con 永远不会从 null 更改为其他任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2014-11-13
    • 2011-09-03
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    相关资源
    最近更新 更多