我正在尝试在 jsp 的下拉列表中加载 mysql 数据库中的值。运行时我遇到了一些异常。谁能帮我解决这个问题。我使用 Department bean 和 adddepartment.jsp 文件作为演示层。
<form action="LoginServlet" method="POST" id="theform" name="theform">
<table cellspacing="20" cellpading="10">
<tr>
<td>Department Id:</td>
<td><input type="text" name="departmentId" id="userName"/></td>
</tr>
<tr>
<td>Department Name:</td>
<td><input type="password" name="departmentName" id="password"/></td>
</tr>
<tr>
<td>Department Name:</td>
<td><input type="password" name="departmentName" id="password"/></td>
</tr>
<tr>
<td>Department Head ID:</td>
<td><select name="departmentHeadId" style="width: 158px"> <option value="0">Select Department Head</option>
<%
//if(request.getParameter("btnvew")!=null)
{
String val="";
Department dp=new Department();
ResultSet rs=dp.addHeadId();
while(rs.next())
{
val=rs.getString(1);
%>
<option value="<%= val%>"><%= val%></option>
<%
}
}
%>
</select></td>
</tr>
<tr>
<td>Appointed Date:</td>
<td><input id="example6" type="text"></td>
</tr>
<tr>
<td><input type="submit" value="Login"/></td>
</tr>
</table>
org.apache.jasper.JasperException: An exception occurred processing JSP page /AddDepartment.jsp at line 52
49: String val="";
50: Department dp=new Department();
51: ResultSet rs=dp.addHeadId();
52: while(rs.next())
53: {
54: val=rs.getString(1);
55:
Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.AddDepartment_jsp._jspService(AddDepartment_jsp.java:170)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
public class Department {
private Connection con;
public ResultSet addHeadId() throws SQLException {
ResultSet rs;
String query = "select * from department_heads";
Statement stmt = con.createStatement();
rs = stmt.executeQuery(query);
return rs;
}
}