-2

THIS CODE IS NOT WORKING, PLEASE HELP !!!

ArrayIndexOutOfBoundException is thrown by compiler, Please HELP !!!

for(var i=1;i<='<%=count%>';+++)
{
var x = document.getElementById("person");
var opt =  document.createElement("option");
opt.text = '<%=list[i]%>';          <------------------ ERROR IS AT THIS LINE
opt.value = '<%=list[i]%>'; 
x.add(opt);
}
}
akash
  • 22,664
  • 11
  • 59
  • 87
mayur saini
  • 209
  • 7
  • 17
  • Are you sure it should be `I <= '<%=count%>'` instead of `I < '<%=count%>'`? – Manu Dec 31 '15 at 13:59
  • This is a **Java** question hiding behind a `javascript` tag. JavaScript doesn't **have** `ArrayOutOfBoundsException`. **Java** does. They're different languages, with different (though similar) syntax, and different (not very similar) models of operation. If we assume that your Java code has `list` and it's an **array** (not a list) and that `count` comes from `list.length`, then this is [a duplicate of this question](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it). – T.J. Crowder Dec 31 '15 at 14:04
  • 1
    Well, mostly. You're also assuming `i` in your JSP code (on the server, as the page is getting ready to be sent to the client) will magically be `i` in your JavaScript code (on the client, later, when the page is loaded and the client-side code is run). – T.J. Crowder Dec 31 '15 at 14:06
  • please, please, please visit this page - http://stackoverflow.com/questions/34553705/local-variable-cannot-be-resolved. It is having the full code, and my refined question... PLEASE HELP ME SIR !!! I HAVE TO SUBMIT THIS PROJECT !! – mayur saini Jan 01 '16 at 05:14
  • Local VARIABLE CANNOT BE RESOLVED? http://stackoverflow.com/q/34553705/5734115?sem=2 PLEASE PLEASE PLEASE HELP ME... – mayur saini Jan 01 '16 at 05:30

1 Answers1

1

I guess this is what u mean. What is count?

var x = document.getElementById("person");
for(var i = 0; i < '<%=count%>';i++) {
   var opt =  document.createElement("option");
   opt.text = '<%=list[i]%>';
   opt.value = '<%=list[i]%>'; 
   x.add(opt);
}
Simone Zandara
  • 9,401
  • 2
  • 19
  • 26
  • here, count is a variable used in the same jsp page, and its value is equal to the number of rows in the database, and the list is a String array variable which is holding the items or the value of a particular column of a database, through getString() method. Please help me !! – mayur saini Dec 31 '15 at 14:58
  • this is a common error(catch(ArrayIndexOutOfBoundsException) which comes whenever m trying to iterate using for loop, and assigning the values of a column from a table of a database, and setting the limit for - for loop as the number of rows... please help me !!! – mayur saini Dec 31 '15 at 15:00
  • I cannot help you more than this. You have to show what list and count are generated. Did you try the solution which I posted? – Simone Zandara Dec 31 '15 at 15:59
  • please, please, please visit this page - stackoverflow.com/questions/34553705/…. It is having the full code, and my refined question... PLEASE HELP ME SIR !!! – mayur saini Jan 01 '16 at 05:15