Results 1 to 8 of 8
- 08-05-2010, 11:26 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
Array index out of bound exception error
Hi Friends,
Please help me to solve this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at RWP.main(Main.java:6)
Here is the part of my code:
Java Code:class RWP{ static Controller ctl; public static void main(String argv[]) { ctl=new Controller(); int n=Integer.parseInt(argv[0]); int[] a; a=new int[10]; for(int i=1;i<=n;i++) { a[i-1]=Integer.parseInt(argv[i]); } new writer(ctl).start(); new reader(ctl).start(); } }
Thanks in advance
- 08-05-2010, 01:58 PM #2
in the logic of your code the first argument passed to RWP is used as the number of arguments. so if you call RWP 1 1 all went well, because
a[i-1]=Integer.parseInt(argv[i]);
will find 1 argument. but if you call RWP 2 1 when you try to read two arguments and you will get an ArrayIndexOutOfBoundsException, because there is only one argument. other error: when you loop through an array the maximum of index must be maximum - 1.
so, correct your code and make sure the number of arguments correspond to the number of passed arguments before you try to assign it.
your post is in the wrong sub-forum.
- 08-05-2010, 03:20 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
Array index out of bound exception error
Thanks Pal....
But could you please help me in correcting that code so that I can have better idea on solving this issue. Can I have that code corrected ?
As I am a beginner it will be highly appreciating if you could help me on this.Last edited by rahulkrishnanr; 08-05-2010 at 03:26 PM. Reason: missed
- 08-05-2010, 05:04 PM #4
Since the args array being passed to the main() method can be empty (ie 0 elements) you need to test the array's length before using it. If it is 0 then it is empty.
- 08-05-2010, 05:04 PM #5
no problem. here is the code. i didn't know what the Controller is so i commented out the lines that refer to this class. further, the RWP runs only when you pass the number of arguments and the argument itself. so try to compile and then call it with java RWP 2 3 4
Java Code:class RWP { // static Controller ctl; public static void main(String argv[]) { // if no parameters are passed the program will exit // with a usage message if (argv.length == 0) { System.out.println("usage: RWP numberOfArguments args"); System.exit(1); } // ctl=new Controller(); int n = Integer.parseInt(argv[0]); int[] a; a = new int[10]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(argv[i+1]); } for (int i = 0; i < n; i++) { System.out.println("a[" + i + "] = " + a[i]); } // new writer(ctl).start(); // new reader(ctl).start(); } }
- 08-06-2010, 09:06 AM #6
Member
- Join Date
- Aug 2010
- Posts
- 3
- Rep Power
- 0
Array index out of bound exception error
Thanks a lot dude. That was really helpful.
- 10-12-2010, 02:42 PM #7
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Spam reported.
-
Similar Threads
-
Array index Out of bound Exception
By nitin_daviet88 in forum New To JavaReplies: 9Last Post: 07-28-2010, 05:32 AM -
Array Index Out Of Bounds Exception
By manowar689 in forum New To JavaReplies: 3Last Post: 06-18-2010, 11:25 PM -
array Index out of Bounds exception== 0
By Allgorythm in forum New To JavaReplies: 6Last Post: 02-11-2010, 04:02 PM -
Array Index Out of Bounds Exception
By kool001 in forum New To JavaReplies: 1Last Post: 12-03-2009, 07:42 AM -
Array Index Out of bound exception
By abhijit in forum NetworkingReplies: 7Last Post: 09-25-2009, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks