我下面的 servlet 正在触发一个空指针异常,但服务器控制台没有详细说明异常是从哪里触发的。此外,链接到 servlet 的表单所在的页面也不会加载,只会显示关于空指针异常的 HTTP 状态 500 错误。下面我复制了我的 servlet 和数据库连接代码,但真的可以看到错误来自哪里?
有人可以看看我的代码,看看是否有任何直接的问题?
我的小服务程序:
@WebServlet(name = "DateSearchServlet", urlPatterns = {"/Search"})
public class DateSearchServlet {
DatabaseConnector dataManager;
boolean dbOK = false;
HomeList homes = new HomeList();
public void init() {
dataManager = new DatabaseConnector();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String dateFrom = request.getParameter("from");
String dateTo = request.getParameter("to");
if (dateFrom == null || dateTo == null) {
dbOK = false;
} else {
dbOK = true;
}//end if
if (dbOK) {
homes = dataManager.getPropertiesSearch(dateFrom, dateTo);
request.setAttribute("homeList", homes);
request.getRequestDispatcher("bookings.jsp").forward(request, response);
} else {
System.out.println("DBNOTOK");
//will add error messages etc...
}
}
}
JSP 页面:
<div class="dateSearch">
<form name="dateSearch" action="DateSearchServlet" method="post">
<label for="from">Date From</label>
<input type="text" id="from" name="from" />
<label for="to">Date To</label>
<input type="text" id="to" name="to" />
<input type="submit" id="submit">
</form>
</div>
<h2>List of Properties</h2>
<%
HomeList homes = (HomeList)request.getAttribute("homeList");
%>
xml文件:
<servlet>
<servlet-name>DateSearchServlet</servlet-name>
<servlet-class>DB.DateSearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DateSearchServlet</servlet-name>
<url-pattern>/Search</url-pattern>
</servlet-mapping>
数据库搜索方法:
public HomeList getPropertiesSearch(String from, String to) {
HomeList homes = new HomeList();//UserBean object to hold the user data
Connection conn = null;
conn = createConnection(conn);//set up the connection to the datbase
//if connection is sucessful, create empty ResultSet and PreparedStatement
if (conn != null) {
ResultSet rs = null;
PreparedStatement preparedStatement = null;
try {
String strQuery =
"SELECT homes.home_id, homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft, "
+ "listagg(features.feature_name, '\\n') WITHIN GROUP(ORDER BY features.feature_name) features, home_type.type_name\n"
+ "FROM homes "
+ "INNER JOIN bookings ON bookings.home_id <> homes.home_id"
+ "INNER JOIN home_feature ON homes.home_id = home_feature.home_id"
+ "INNER JOIN home_type ON home_type.type_code = homes.type_code"
+ "INNER JOIN features ON home_feature.feature_id = features.feature_id"
+ "WHERE bookings.booking_end < date '?'"
+ "OR bookings.booking_start > date '?'"
+ "GROUP BY homes.home_id, homes.title, homes.description, homes.living_room_count, homes.bedroom_count, homes.bathroom_count, homes.price, homes.sqft, home_type.type_name";
preparedStatement = conn.prepareStatement(strQuery);//prepare the statement
preparedStatement.setString(1, to);//insert password into query
preparedStatement.setString(2, from);//insert password into query
rs = preparedStatement.executeQuery();//execute the query and store this into a ResultSet
homes = new HomeList(rs);
}//end try
catch (SQLException ex) {
ex.printStackTrace();
} finally {
closeConnection(conn);//call close connection the close the database connection
}//end finally
}//end if
conn = closeConnection(conn);
return homes;
}
谢谢,如果有人能指出我正确的方向
堆栈跟踪:
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.bookings_jsp._jspService(bookings_jsp.java:191)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)