Connecting Tech Pros Worldwide Help | Site Map

index out of bound error

Member
 
Join Date: Dec 2007
Posts: 66
#1: Jun 15 '08
Expand|Select|Wrap|Line Numbers
  1.  
  2. public class settasks extends BLTHAdapter {
  3.      String subroleArr[];
  4.      static int i=0;
  5.      static int j;
  6.     public void handleSetSubject(BLTHContext ctx) throws Exception 
  7.     {
  8.     try{
  9.                         ..... some code   
  10.     for(j=0;j<v.size();j++,i++)
  11.     {    
  12.  
  13.         subroleArr[i]=some string
  14.        getsubroleArr(subroleArr[i],ctx);               
  15.  
  16.     }
  17.  
  18.     adminrole.setRoleDescription(subroleArr[0]);
  19. }
  20. catch(Exception e)
  21.     {
  22.     throw e;
  23.  
  24.     }
  25. }
  26. public void getsubroleArr(String v,BLTHContext ctx1)throws Exception
  27. {
  28. ..some code    
  29. while(some condition )         
  30.  {
  31.  
  32. i++;
  33. subroleArr[i]=some string
  34. getsubroleArr(subroleArr[i],ctx1); //function called recursively
  35.  
  36. }
  37. }        
  38.  
  39.         }
  40.  
The above code is wrtitten using API's .I have written a recursive function getsubrolArr() , this recursive fucntion is storing values in a array. For counter of the array i have used a static variable i.
But when I am trying to increment i in the function getsubrole. I am getting "java.lang.ArrayIndexOutOfBoundsException: 2"
Is there any thing wrong in the incrementing i in the above code.
Any pointers will be very helpful
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Jun 16 '08

re: index out of bound error


I have no idea what your code is supposed do, what this immediately caught my eye:
Expand|Select|Wrap|Line Numbers
  1. static int i=0;
  2. static int j;
Static loop variables? In all my years of programming I've never seen that attempted. Could this be your error?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Jun 16 '08

re: index out of bound error


What did you initialize subroleArr[]'s length to be?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 16 '08

re: index out of bound error


Quote:

Originally Posted by r035198x

What did you initialize subroleArr[]'s length to be?

I don't think that matters much because that code shows that the index variable 'i'
is only incremented and never reset to a sensible value, so once 'i' will be larger
than the maximum array index. (ignoring an array having Integer.MAX_VALUE
elements)

kind regards,

Jos
Reply