Struts2 -- NullPointerException from radio button -
i have following form in struts2 jsp contains radio buttons. there 2 other forms on page work correctly, 1 doesn't. have determined value selected somehow not being passed, , that reason i'm getting nullpointerexception, can't figure out why it's happening. can me? here jsp form.
<s:form action="processpoll3"> <table> <tr> <td><b><i>poll #3</i></b></td> <td>how many kids have?</td> <td><s:radio name="poll3" list="#{'1':'0.', '2':'1.', '3':'2.', '4':'3.', '5':'more 3.'}" /> <s:submit value="vote poll #3" align="left" /></td> </tr> </table> </s:form>
my dao class gets called line (and being called, sure): string poll3; private httpservletresponse response;
public string getpoll3() { return poll3; } public void setpoll2(string poll3) { this.poll3 = poll3; } public string execute() { poll3dao poll3dao = new poll3dao(); if (poll3dao.tallyvote(poll3).equals("success")) { // processing goes on here, not relevant problem }
here method in dao class, breakdown point marked because parameter supposed passed null.
public string tallyvote(string vote) { string successfulwrite; request = servletactioncontext.getrequest(); sessionfactory sessionfactory = (sessionfactory) request.getsession() .getservletcontext().getattribute("sessionfactory"); session session = sessionfactory.opensession(); try { // previous results transaction tx1 = session.begintransaction(); query myquery = session.createquery("from poll3"); tx1.commit(); // update results iterator<poll3> iterate = myquery.iterate(); poll3 poll3 = iterate.next(); // nullpointerexception occurs on next line if (vote.equals("1")) { poll3.setzero(poll3.getzero() + 1); } else if (vote.equals("2")) { poll3.setone(poll3.getone() + 1); } else if (vote.equals("3")) { poll3.settwo(poll3.gettwo() + 1); } else if (vote.equals("4")) { poll3.setthree(poll3.getthree() + 1); } else if (vote.equals("5")) { poll3.setmorethanthree(poll3.getmorethanthree() + 1); } // write new results database; transaction tx2 = session.begintransaction(); session.update(poll3); tx2.commit(); successfulwrite = "success"; } catch (exception e) { system.out.println(e.tostring()); successfulwrite = "failure"; } return successfulwrite; }
i'm betting it's this:
public void setpoll2(string poll3) { ... }
this why have map/collection support avoid writing cut-and-paste blobs this.
any time find cutting , pasting code in snippets it's because abstraction has been ignored/overlooked.
Comments
Post a Comment