Results 1 to 5 of 5
  1. #1
    dropt is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default Array out of bounds exception 20.

    Hey there, fairly new to Java and taking a course in said subject.

    Having to write a program that analyzes and manipulates a couple of integer arrays. I'm having issues with a java.lang.ArrayIndexOutOfBoundsException: 20 error. I've got some c/c++ background so it may be that I am trying something that would work in that environment and not in Java. Any help or insight would be great.

    Java Code:
    public static void invert(int invIn[], int invOut[]){
      System.out.println();              //new line as a break.
      int inv=0;
      if (invIn.length == invOut.length){
    
      for (int i=0; i<=invIn.length; i++){
      
        invOut[i]=invIn[i] * -1;
      
      
      /* Alternate attempt
      int inv=0;
      inv=invIn[i];
      inv=inv * -1;
      invOut[i]=inv;
      end Alternate attempt*/
      
      }//end for loop
     
      }else if(invIn.length != invOut.length){
        System.out.println("Array size unequal, aborting procedure");
      }
    
      
    //}//end invert for loop
    
    }//end invert
    The error occurs when the program gets to

    invOut[i]=invIn[i] * -1;

    I'm trying to invert the invIn array and fill invOut with those inverted values.

    Thanks for your time!

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Does invIn or invOut contain any null values?

    That looks like what your problem is.

  3. #3
    dropt is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    invIn is passed in from outside of the procedure as.

    Java Code:
    int w1[]={10,5,-3,-9,-8,0,8,9,3,-5,-10,-5,3,9,8,0,-8, -9,-3,5};
    
    ...
    
    int w4[];
    w4=new int[w1.length];
    invert(w1, w4);
    so w1 shouldn't have any null values as it itself is never changed.

    w4 is an int array which is (i assume) comprised of null values. However I'm not performing the mathematical operation on w4, only w1 as shown.


    Thanks for your timely response.

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Oh, oops.

    That would have been a null pointer exception.

    It's because you are going out of bounds, hence the ArrayIndexOutOfBounds exception; not the NullPointerException I thought I saw ;)

    change <= invIn.length to < invIn.length

    Mr. Beans

  5. #5
    dropt is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks Dr. Beans, much appreciated. I could have stared at it all day and not caught that!

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, 10:16 AM
  2. Replies: 8
    Last Post: 09-10-2009, 09:31 PM
  3. Java ArrayList out of bounds exception
    By grahamb314 in forum New To Java
    Replies: 5
    Last Post: 11-22-2008, 07:21 PM
  4. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 08:16 AM
  5. why is my array out of bounds?
    By Phobos0001 in forum New To Java
    Replies: 3
    Last Post: 03-24-2008, 01:20 AM

Posting Permissions

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