0

以下のサーブレットはnullポインター例外を発生させていますが、サーバーコンソールは例外が発生している場所の詳細を示していません。また、サーブレットにリンクされているフォームがオンになっているページは読み込まれず、nullポインタ例外に関するHTTPステータス500エラーが表示されるだけです。以下にサーブレットとデータベース接続コードをコピーしましたが、エラーの原因を実際に確認できますか?

誰かが私のコードを見て、差し迫った問題があるかどうかを確認できますか?

私のサーブレット:

@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)
4

1 に答える 1

0

init() メソッドを呼び出すのはいつですか? ほとんどの場合、呼び出されることはなく、次のdataManager行で が null であるため、NPE が発生します。

 homes = dataManager.getPropertiesSearch(dateFrom, dateTo);
于 2013-01-22T16:27:07.213 に答える