Stacktrace:]带有根本原因java.lang.NullPointerException

时间:2016-01-13 11:21:07

标签: jsp pointers jdbc null

我在代码的这一点上遇到错误

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>

1 个答案:

答案 0 :(得分:0)

永远不会为您的成员变量con分配值null

init()方法中,您声明了本地变量,因此成员变量 Connection con永远不会从null更改为别的什么。