Results 1 to 9 of 9
  1. #1
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default array index out of bounds exception, AGAIN!

    I am using this code, somewhere in the middle of a huge program.

    Java Code:
     else if(ev.getSource() == okButton1){ 
            if(a<neigh1.s)a++;
    for( int i=0; i<(810/30); i++) {
                for( int j=0; j<(630/30); j++) {
                    rules[a-1][i][j]=exch3.rulbas[i][j];}}}
    And I am getting an out_of_bounds exception. Is this command allowed? Where could my array possibly be going awry?
    I have initialized them as follows:
    Java Code:
    public static int a=0;
        public rule2 exch3;
        public static int rules[][][]= new int[neigh1.s][810/30][630/30];
    and
    Java Code:
    class rule2   implements Serializable {
          public int rulbas[][];....etc

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    1. Learn to not use magic numvbers.
    Java Code:
    for( int i = 0; i < rules[a-1].length; i++) {
        for( int j = 0; j < rules[a-1][i].length; j++) {
               rules[a-1][i][j] = exch3.rulbas[i][j];
        }
    }
    2. Learn how to format code for readability: Code Conventions for the Java(TM) Programming Language: Contents

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Is this command allowed?
    Java has statements. Not commands.

    db

  4. #4
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    Okay. Well I didn't use magic numbers in the statement, simply replaced the variable with the value it contained for better understanding.
    And the code fragment didn't work, obviously, since one variable was simply replaced with another. I am asking if there is some hierarchical issues I haven't thought about.
    Also is there a simpler way of putting the values of many 2D matrices into a 3D matrix?

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Quote Originally Posted by samanyu View Post
    Well I didn't use magic numbers in the statement, simply replaced the variable with the value it contained for better understanding.
    That creates a worse understanding. Nobody can tell where those values might have come from, or what they might represent. Giving every variable a meaningful name will actually help you avoid errors like this.
    Get in the habit of using standard Java naming conventions!

  6. #6
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    OK. i apologise. Is the code inside the nested for loops correct? Can I do that?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    Can I do that?
    The code appears to be copying the value from one array to another. Yes you can do that if ....
    both arrays are defined with the correct number of dimensions
    indexes are in bounds
    types of the two arrays are compatible

  8. #8
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    OK. I'll work on the indexes and get back to you.

  9. #9
    samanyu is offline Member
    Join Date
    Jun 2011
    Location
    Tsukuba, Japan
    Posts
    63
    Rep Power
    0

    Default

    Yes, I solved the problem using a print option after every step, I saw where I was going wrong. Thanks to DarrylBurke and Norm. Cheers.

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 11:16 AM
  2. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 11:24 PM
  3. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-19-2010, 12:25 AM
  4. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 05:02 PM
  5. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 08:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •