Stacktrace:] 根本原因是 java.lang.NullPointerException
Posted
技术标签:
【中文标题】Stacktrace:] 根本原因是 java.lang.NullPointerException【英文标题】:Stacktrace:] with root cause 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: %>
<!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>
【问题讨论】:
What is a Null Pointer Exception, and how do I fix it?的可能重复 【参考方案1】:您的成员变量con
从未被赋值,因此null
在您的 init()
方法中,您声明了一个 local 变量 con - 因此 成员变量 Connection con
永远不会从 null
更改为其他任何内容。
【讨论】:
以上是关于Stacktrace:] 根本原因是 java.lang.NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章