mysql - Java returning null pointer exception for SQL query that gets passed to JSP -


i working on school assignment required use sql statements in java code use like operator search. in order search have string user, , split string delimiter, , run query so:

select * movies (movies.title '%userinput%');

i return query in form of arraylist.

now, when testing out. tested no user input, , query became: select * movies (movies.title '%%');. gave me correct results.

however when put title in there, of sudden nullpointerexception on line:

if(title.equals("")) { return "(movies.title '%%') "; section of code:

public string getsearchstring(string title) {     if(title.equals("")) { return "(movies.title '%%') "; }     string ret = "(";     arraylist<string> titlearray = util.splitsearch(title);     for(int = 0; < titlearray.size() - 1; ++i) {         string temp = titlearray.get(i);         string stmt = "movies.title '%" + temp + "%' or ";         ret += stmt;     }     string temp = "movies.title '%" + titlearray.get(titlearray.size() - 1) + "%')";     ret += temp;     return ret; } 

this called so:

public list<movie> listmovies(string title) throws sqlexception {     list<movie> search = new arraylist<movie>();     if(null != title && title.isempty()) { title = ""; }     resultset res = querymovies(getsearchstring(title));     while(res.next()) {         movie mov = new movie();         mov.settitle(res.getstring("title"));         search.add(mov);     }     return search; }  private static querymovies(string st) throws sqlexception {     resultset res = null;     try {         preparedstatement ps = dbcon.preparestatement(st);         res = ps.executequery();     } catch(sqlexception e) {         e.printstacktrace();     }     return res; } 

i unfortunately have since won't know how user enter. , not allowed use external libraries make formatting easier. reference util.splitsearch(...) method looks this. should retrieving alphanumeric character , should splitting on not alphanumeric:

public static arraylist<string> splitsearch(string str) {     string[] strarray = str.split("[^a-za-z0-9']");     return new arraylist(arrays.aslist(strarray)); } 

what interesting when pass in getsearchstring(""); explicitly, not nullpointerexception. when allows variable title used one. , still 1 when no string entered.

am splitting string wrong? somehow giving sql wrong statement? appreciated, new this.

the "title" passed input null, hence you're getting nullpointerexception when title.equals("").

best practices suggest null check (null != title && title.equals("")). can "".equals(title)


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -