1

ユーザーが JSP ページで行った日付の選択に基づいて、データベースから値を取得しようとしています。ユーザーが 2 つの日付を選択すると、データベースが照会されます。

私のサーブレットでは、これを行っています。for ステートメントは、リストに正しい値があることを示しています。

VehiclesList list = dataManager.getVehiclesWhere(dateFrom, dateTo);

request.setAttribute("vehiclesList", list);

VehiclesList vList = (VehiclesList) request.getAttribute("vehiclesList");

for (int pos = 0; pos < vList.size(); pos++) {
        Vehicles vehicles = vList.getVehicleAt(pos);
        System.out.println(vehicles.getC02Emissions());
        System.out.println(vehicles.getManufacture());
        System.out.println(vehicles.getRegNumber());
        System.out.println(vehicles.getHireRate());
}

RequestDispatcher dispatcher = request.getRequestDispatcher("/vehicles.jsp");
dispatcher.forward(request, response);

これは私の JSP ページのコードです。上記と同じプロセスを実行しています。つまり、属性を要求し、新しいリストを作成してデータを表示していますが、作成している新しいリストが null になり、null ポインター例外を受け取ります。

VehiclesList vList = new VehiclesList();
vList = (VehiclesList) request.getAttribute("vehiclesList");

for (int pos = 0; pos < vList.size(); pos++) {
     Vehicles vehicle = vList.getVehicleAt(pos);
     System.out.println(vehicle.getC02Emissions());
}

サーブレット側では正しい値を保持しているのに、JSP 側のリストが null なのはなぜですか?

編集 - - -

これは私のスタックトレースです。

    WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.vehicles_jsp._jspService(vehicles_jsp.java from :257)
    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:680)

編集2------------

サーブレット コード

package Oracle;

import RentalRecords.Vehicles;
import RentalRecords.VehiclesList;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class VehiclesServlet extends HttpServlet {

    private boolean dbOK = false;//boolean to check if data input is not null
    private Vehicles vehicle;//used to get user data from bean
    private DatabaseConnector dataManager;//used to interact with the database

    /**
     *
     * Initialises the servlet by creating a new DatabaseConnector object which
     * will be used to interface with the database
     */
    public void init() {
        dataManager = new DatabaseConnector();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String dateFrom = request.getParameter("from");//get email submitted from form
        String dateTo = request.getParameter("to");//get password submitted from form
        System.out.println(dateFrom + dateTo);



        if (dateFrom != null || dateTo != null) {
            dbOK = true;
        } else {
            dbOK = false;
        }//end if

        HttpSession session = request.getSession(true);//set the session


        if (dbOK) {
            try {
                System.out.println(dbOK);
                VehiclesList list = dataManager.getVehiclesWhere(dateFrom, dateTo);

                request.getSession().setAttribute("vehiclesList", list);

                VehiclesList vList = (VehiclesList) request.getAttribute("vehiclesList");

                for (int pos = 0; pos < vList.size(); pos++) {
                         Vehicles vehicles = vList.getVehicleAt(pos);
                         System.out.println(vehicles.getC02Emissions());
                         System.out.println(vehicles.getManufacture());
                         System.out.println(vehicles.getRegNumber());
                         System.out.println(vehicles.getHireRate());
                }

                RequestDispatcher dispatcher = request.getRequestDispatcher("/vehicles.jsp");
                dispatcher.include(request, response);
                //request.getRequestDispatcher("vehicles.jsp").forward(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }//end try
        } else {
            //User is not valid, redirect back to index.jsp and inform user
            session.setAttribute("signupError", "Signup Not Successful");
            session.setAttribute("signupSuccess", null);//set this null if it is a resubmit
            System.out.println(session.getAttribute("signupError"));
            RequestDispatcher dispatcher = request.getRequestDispatcher("/vehicles.jsp");
            dispatcher.forward(request, response);
        }

    }
}

JSP コード

<%@page import="RentalRecords.Vehicles"%>
<%@page import="RentalRecords.VehiclesList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <script>
            $(document).ready(function() {
                var from = $( "#from" ).datepicker({
                    defaultDate: "+1w",
                    changeMonth: true,
                    numberOfMonths: 1,
                    minDate: 0,
                    dateFormat: 'd-M-y',
                    onClose: function( selectedDate ) {
                        $( "#to" ).datepicker( "option", "minDate", selectedDate );
                    }

                });
                var to = $( "#to" ).datepicker({
                    defaultDate: "+1w",
                    changeMonth: true,
                    numberOfMonths: 1,
                    minDate: 0,
                    dateFormat: 'd-M-y',
                    onClose: function( selectedDate ) {
                        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
                    }
                });
            });
        </script>
    </head>
    <body>
        <form id="getdates" method="get" action="getdates">
            <label for="from">From</label>
            <input type="text" id="from" name="from" />
            <label for="to">to</label>
            <input type="text" id="to" name="to" />
            <input id="submitdates" name="submitdates" type="submit" value="Search" />
        </form>
        <div class="header_01">Vehicles</div>

        <%


            VehiclesList vList = new VehiclesList();
            vList = (VehiclesList) request.getSession().getAttribute("vehiclesList");

            for (int pos = 0; pos < vList.size(); pos++) {
                Vehicles vehicle = vList.getVehicleAt(pos);
                System.out.println(vehicle.getC02Emissions());


        %>

        <table>
            <tr>
                <td class="label"><label id="ltable" for="table"></label>
                    <table>
                        <tr>
                            <td class="label"><label id="lbodytype" for="transmission">Body Type</label></td>
                            <td class="field"><input id="bodytype" name="transmission" type="text" value="<%= vehicle.getBodyType()%>" maxlength="50" readonly/></td>
                            <td class="status"></td>
                        </tr>
                        <tr>
                            <td class="label"><label id="ltransmission" for="transmission">Transmission</label></td>
                            <td class="field"><input id="transmission" name="transmission" type="text" value="<%= vehicle.getTransmission()%>" maxlength="50" readonly/></td>
                            <td class="status"></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
        <% }%>
    </body>
</html>
4

1 に答える 1

2

jspページでnullを確認してください

if(request.getAttribute("vehiclesList") !=null)
{
 VehiclesList vList = new VehiclesList();
 vList = (VehiclesList) request.getAttribute("vehiclesList");

  for (int pos = 0; pos < vList.size(); pos++) {
     Vehicles vehicle = vList.getVehicleAt(pos);
     System.out.println(vehicle.getC02Emissions());
  }
}
于 2013-01-23T09:57:00.827 に答える