java - Getting a null value on my arraylist.set(). using jsp -


my objective add words html form, store them in array, display them in page, , have 2 links 1 delete item , 1 edit item(the word edit entered in html form).

i using arraylist store input coming input type. able add , delete item, when tried change value of item in arraylist (i using arraylist.set()), value returns null. if hard code set() see item change reason can't value input type. wondering missing.

i have 2 jsp pages. main.jsp , edit.jsp. 1 java class wordoperations validations

here code sorry if messy , if doesn't display right in here. first time posting on stackoverflow

any appreciated. thanks

this code form display items main.jsp

 <h3>word list</h3>         <form action="wordoperations" method="post">             word: <input type="text" name="newword">             <input type="submit" value="add">         </form>         <br/>              <%                 arraylist<string> words = (arraylist<string>) session.getattribute("words");                  if (words!=null)                 {                     (int i=0; i<words.size(); i++)                     {                          out.println("<p>" +  "<a href='http://www."+ words.get(i) +".com'>" + words.get(i) +"</a>"  + "&nbsp;&nbsp;<a href='wordoperations?delete=" + + "'>delete</a>&nbsp;&nbsp;<a href='wordoperations?edit=" + + "'>edit</a></p>");                      }                 }              %> 

this code enter new information edit.jsp

  <form action="wordoperations?" method="post">         new link:<input type="text" name="userchange">         <input type="submit" name="userchangepassword" value="change link">         </form> 

and code validation of forms wordoperations.java

 httpsession session = request.getsession();          string newword = request.getparameter("newword");         string delete = request.getparameter("delete");         string edit = request.getparameter("edit");         string newlink = request.getparameter("userchange");          if (delete!=null) //delete word         {             int delindex = integer.parseint(delete);              arraylist<string> words = (arraylist<string>) session.getattribute("words");             words.remove(delindex);              requestdispatcher rd = getservletcontext().getrequestdispatcher("/main.jsp");             request.setattribute("message", "word deleted");             rd.forward(request, response);           }         else if(edit !=null)         {                  int edindex = integer.parseint(edit);             string link = newlink;             arraylist<string> words = (arraylist<string>) session.getattribute("words");             words.set(edindex,"google");             words.add(newlink);                          requestdispatcher rd = getservletcontext().getrequestdispatcher("/edit.jsp");               request.setattribute("message", "word edited");             rd.forward(request, response);          }         else //logging in         {             if (newword!=null && !newword.equals(""))             {                 arraylist<string> words = (arraylist<string>) session.getattribute("words");                  words.add(newword);                                requestdispatcher rd = getservletcontext().getrequestdispatcher("/main.jsp");                 request.setattribute("message", "word added");                 rd.forward(request, response);              }             else             {                 requestdispatcher rd = getservletcontext().getrequestdispatcher("/main.jsp");                 request.setattribute("message", "word required");                 rd.forward(request, response);             }         } 

it's logical mistake ..

you running servlet wordoperations.java twice in editing word , passing edit parameter once , passing userchange parameter in , not solve problem.

here mistake.

but first set arraylist words in session on adding of first word. can this:

else //logging in             {                 if (newword != null && !newword.equals("")) {                     arraylist<string> words = (arraylist<string>) s.getattribute("words");                     if (words == null) {                         words = new arraylist<>();                         words.add(newword);                         s.setattribute("words", words);                     } else {                         words.add(newword);                     }                      requestdispatcher rd = getservletcontext().getrequestdispatcher("/main.jsp");                     request.setattribute("message", "word added");                     rd.forward(request, response);                  }  

now here mistake :

when arraylist words listed down in main.jsp. contains

word delete_link edit_link

now delete_link > <a href='wordoperations?delete=" + + "'>delete</a> working fine because redirecting wordsoperations.java parameter delete = i , deleting word arraylist.

wordsoperations.java

string delete = request.getparameter("delete");   if (delete!=null) //delete word         {             int delindex = integer.parseint(delete);              arraylist<string> words = (arraylist<string>) session.getattribute("words");             words.remove(delindex);              requestdispatcher rd = getservletcontext().getrequestdispatcher("/main.jsp");             request.setattribute("message", "word deleted");             rd.forward(request, response);           } 

but

in edit_link > <a href='wordoperations?edit=" + + "'>edit</a> calling servlet wordsoperations.java parameter edit = i , not passing userchange parameter it.

thus newlink null on

string newlink = request.getparameter("userchange");

so code in wordsoperation.java i.e

else if(edit !=null)         {                  int edindex = integer.parseint(edit);             string link = newlink;             arraylist<string> words = (arraylist<string>) session.getattribute("words");             words.set(edindex,"google");             words.add(newlink);                          requestdispatcher rd = getservletcontext().getrequestdispatcher("/edit.jsp");               request.setattribute("message", "word edited");             rd.forward(request, response);          } 

will executed edit!=null true. add newlink in arraylist words happens null. hence null getting added list.

there no problem arraylist.set() method.

now in above code calling edit.jsp after adding newlink. when wordsoperation.java gets called edit.jsp form, passing userchange parameter , not edit parameter needed there execute else if(edit!=null) block of servlet wordsoperations.java.

<form action="wordoperations?" method="post">             new link:<input type="text" name="userchange">             <input type="submit" name="userchangepassword" value="change link">         </form> 

rest on you. take care pass edit , userchange parameter both servlet wordsoperations.java work done.

suggestion: pass edit parameter edit.jsp , pass edit , userchange parameter wordsoperation.java.

hope helps.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -