从getters方法获取null

时间:2015-04-29 14:22:22

标签: java servlets

尝试将其保存在列表中并在servlet中调用列表时,我从getters方法获取null。请帮我摆脱这个。提前致谢。 我首先添加getter和setter方法代码然后添加DAO代码然后添加servlet代码。

package com.vechile.model;


public class DriverSearchResult {

    private String Id;
    private String Name;
    private String Address;
    private String City;
    private String Contact;
    private String Country;
    public String getId() {
        return Id;
    }
    public void setId(String id) {
        Id = id;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getAddress() {
        return Address;
    }
    public void setAddress(String address) {
        Address = address;
    }
    public String getCity() {
        return City;
    }
    public void setCity(String city) {
        City = city;
    }
    public String getContact() {
        return Contact;
    }
    public void setContact(String contact) {
        Contact = contact;
    }
    public String getCountry() {
        return Country;
    }
    public void setCountry(String country) {
        Country = country;
    }


}

package com.vechile.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.vechile.model.Driver;
import com.vechile.model.DriverSearchResult;
import com.vechile.model.Searching;
import com.vechile.utils.DBConnection;

public class SearchDAO {


    private String sql;
    private ResultSet rs, rs1;



    public boolean searchDriver(Searching searching) {
        try {

            sql = String.format("select * from adddriver where (%s) = '%s'",
                    searching.getInputList(), searching.getTxtField());

            rs = DBConnection.executeQuery(sql);

            if(rs.next()) {

                DriverSearchResult setResult = new DriverSearchResult();
                Integer id = new Integer(rs.getInt(1));
                String stringID = id.toString();
                setResult.setId(stringID);
                setResult.setName(rs.getString(1));
                setResult.setAddress(rs.getString(2));
                setResult.setCity(rs.getString(3));
                setResult.setContact(rs.getString(4));
                setResult.setCountry(rs.getString(5));

                return true;
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return false;
    }
    public List<DriverSearchResult> getDriverResult(){


            List<DriverSearchResult> returnList = new ArrayList<DriverSearchResult>();
            DriverSearchResult getResult = new DriverSearchResult();
            String id = getResult.getId();
            String name = getResult.getName();
            String address = getResult.getAddress();
            String city = getResult.getCity();
            String contact = getResult.getContact();
            String country = getResult.getCountry();

            returnList.add(getResult);
            return returnList;

    }

    public boolean searchBooking(Searching searching) {
        try {
            sql = String.format(
                    "select * from booking where booking_id = '%s'",
                    searching.getBookingSearch());

            rs1 = DBConnection.executeQuery(sql);
            if (rs1.next()) {

                return true;

            }

        } catch (Exception e) {
            e.getStackTrace();
        }
        return false;
    }


}

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String input = request.getParameter("list");
        String txtField = request.getParameter("txtField");
        String op = request.getParameter("op");
        String bSearch = request.getParameter("bookingSearch");



        try{
            if(op.equals("1")){
                Searching search1 = new Searching();
                search1.setInputList(input);
                search1.setTxtField(txtField);
                SearchDAO searchDAO = new SearchDAO();
                boolean searchDriver = searchDAO.searchDriver(search1);

                if(searchDriver){
                    DriverSearchResult searchResult = new DriverSearchResult();


                        System.out.println(searchResult.getId());

                    //response.sendRedirect("driversearchresult.jsp");


                    //response.sendRedirect("SearchOutPut.jsp?op=" + search1.getPassingValue());

                }
                else {
                    out.println("<html>");
                    out.println("<head>");
                    out.println("<script>");
                    out.println("alert('Record Not Found!')");
                    out.println("</script>");
                    out.println("<META HTTP-EQUIV= Refresh CONTENT=0;URL=SearchDriver.jsp>");
                }

            }

1 个答案:

答案 0 :(得分:3)

这是因为您在方法DriverSearchResult中填充searchDriver对象,然后忘记该对象。然后在另一个方法中创建另一个类DriverSearchResult的实例,并尝试从中提取值而无需事先初始化。这就是为什么它给你空值,因为你从不在这个新创建的实例上调用setter。

我想您要保存DriverSearchResult方法中创建的searchDriver实例,然后在其他方法中使用它而不是创建新方法。

UPD: 我希望从代码片段中可以清楚地看出这个想法:

public class SearchDAO {
    private List<DriverSearchResult> resultList = new ArrayList<>();


    public boolean searchDriver(Searching searching) {
        try {
            // ...
            if(rs.next()) {
                DriverSearchResult setResult = new DriverSearchResult();
                // ...
                resultList.add(setResult); // <<<<<<<<<
                return true;
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return false;
    }

    public List<DriverSearchResult> getDriverResult(){
        return resultList; // <<<<
    }
}