java - Getting null from getters method -
i getting null getters method when trying save in list , calling list in servlet. please me out rid of this. in advance. adding first getters , setters method code dao code , servlet code.
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 * adddriver (%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 * booking 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>"); } }
that happens because fill driversearchresult
object in method searchdriver
, forget object. in method create instance of class driversearchresult
, try extract values without prior initialization. that's why gives nulls, since never call setters on newly created instance.
i guess want save driversearchresult
instance created in searchdriver
method , use in other methods instead of creating new one.
upd: hope idea clear code snippet:
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; // <<<< } }
Comments
Post a Comment