Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Array index out of bound exception

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting Array index out of bound exception so please help in solving the error
int i;
String[] loca=new String[10];
if(request.getParameter("location") != null)
loca = request.getParameterValues("location");

for(i=0;i<=loca.length;i++)
{
out.println("loca"+"<br>"+loca[i]);


}
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on JSP.

This post has been moved to a more appropriate forum.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anu hi

I think the problem is that; in the line

with the specification "loca[i]", you try to use an array element
with an index number 0... An array cant have 0th element...
Its because, at the beginning of your for loop, your index i is
starting with the value 0...
I think, somehow you must avoid using 0th element in the array..
Then the problem drops automatically

Have a beautiful day..
 
Master Rancher
Posts: 4598
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

for(i=0;i<=loca.length;i++)



The last element in the array has an index = length of array - 1

What does the <= test above allow the value of i to be?
 
Gulsum Ramazanoglu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anu hi again my dear, I do apologize for mistakenly misleading you .. An array begins with 0th element and I was wrong in my comment.. Please disregard it.

Ok, from now on I wont be "good" for others for some time.. its rather harmful for I am still a newbie!
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, an array index out of bounds expception actually report the index of the member that is being accessed, when it is printed. You can use this as a way to figure out why the logic is trying to deference an invalid member.

Henry
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just change the code to

i.e. replace the <= with <. This is a very basic mistake.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic