Results 1 to 5 of 5
- 09-21-2009, 11:08 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
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
invOut[i]=invIn[i] * -1;
I'm trying to invert the invIn array and fill invOut with those inverted values.
Thanks for your time!
- 09-21-2009, 11:20 PM #2
Does invIn or invOut contain any null values?
That looks like what your problem is.
- 09-21-2009, 11:23 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
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);
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.
- 09-21-2009, 11:27 PM #4
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
- 09-21-2009, 11:32 PM #5
Member
- Join Date
- Sep 2009
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
[SOLVED] Array index out of bounds exception
By sruthi_2009 in forum New To JavaReplies: 6Last Post: 03-23-2014, 11:16 AM -
Random numbers and an Out of Bounds exception teaming up to annoy me.
By Pugovitz in forum New To JavaReplies: 8Last Post: 09-10-2009, 10:31 PM -
Java ArrayList out of bounds exception
By grahamb314 in forum New To JavaReplies: 5Last Post: 11-22-2008, 08:21 PM -
[SOLVED] out of bounds exception help
By soxfan714 in forum New To JavaReplies: 21Last Post: 11-11-2008, 09:16 AM -
why is my array out of bounds?
By Phobos0001 in forum New To JavaReplies: 3Last Post: 03-24-2008, 02:20 AM
Bookmarks