我正在尝试使用servlet在下拉列表中填充数据库GroupName。怎么做?请帮忙
在我的jsp文件中有:
<% ArrayList<String> list1 = (ArrayList<String>)request.getAttribute("res1"); %> <select name="GName" > <% for(String list:list1) { %> <option><%=list%></option> <% } %>
在我的servlet中,我像这样在arraylist中获取所有数据库名称:
ArrayList<String> list = new ArrayList<String>(); out.println("HI GROUP MEMBER"); String query="Select GNAME from tbGroup"; ps = con.prepareStatement(query); rs=ps.executeQuery(); while(rs.next()){ list.add(rs.getString("GNAME")); } String[] arr1 = list.toArray(new String[list.size()]); request.setAttribute("res1",arr1); int i; for(i=0;i<list.size();i++){ out.println(arr1[i]); }
现在正在获取空指针异常。请帮助可能是什么原因?
这是完整的错误:
Stacktrace:] with root cause java.lang.NullPointerException at org.apache.jsp.GroupLoginScreen_jsp._jspService(GroupLoginScreen_jsp.java:91) 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:419) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 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:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311) 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)
<% } %> </select>
在您的JSP中使用它。Servlet代码
ArrayList<String> list = new ArrayList<String>(); out.println("HI GROUP MEMBER"); String query="Select GNAME from tbGroup"; ps = con.prepareStatement(query); rs=ps.executeQuery(); while(rs.next()){ list.add(rs.getString("GNAME")); } request.setAttribute("res1",list); RequestDispatcher rd= context.getRequestDispatcher("GroupLoginScreen.jsp"); rd.forward(request, response);