java - Null pointer exception in passing checkbox value -


i made index page in take value of checkbox , pass on file named addtowork.java showing null pointer exception. there problem in passing value of checkbox. kindly help. here code snipped index page

<td> <center> <form action="addtowork?id2=<%=mail.gettemptoken()%>" method="post"> <input type="submit" value="add work"> <input type="checkbox" name="flag" value="flag">high priority</form>    </center></td> 

for addtowork.java

protected void dopost(httpservletrequest request, httpservletresponse     response) throws servletexception, ioexception {      emaildesc mail = new emaildesc();      string imp = new string();      imp = (string) request.getparameter("flag");        string thisid = request.getparameter("id2");     home home = new home();      user user = new user();      user = (user) request.getsession().getattribute("user");      mail = home.getemail(thisid, user);        home.givepermanenttoken(mail,thisid);      if (imp.equals("flag")){         system.out.println("priority changed " + mail.getpriority() + "!");     }        response.sendredirect("index1.jsp");    } 

if remove if statement in addtowork.java, code runs perfectly.

case 1: name.equals("java") compare unknown value known value.

we comparing name(unknown) value string java(known) value. name decided based on database call, calling method, etc... may possible null value of name , possible chances of java.lang.nullpointerexception or have check explicitly null value of name.

case 2: "java".equals(name) compare known value unknown value.

we comparing java(known) value string name(unknown) value. same way name decided based on database call, calling method, etc... equals , equalsignorecase method of string handle null value , don't have check explicitly null value of name.

in case

/* getting `null` variable `imp` */ imp = (string) request.getparameter("flag"); 

change

if (imp.equals("flag")){     system.out.println("priority changed " + mail.getpriority() + "!"); } 

to

if ("flag".equals(imp)){     system.out.println("priority changed " + mail.getpriority() + "!"); } 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -