Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    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.

  3. #3
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default 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

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,558
    Rep Power
    23

    Default

    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.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by rahulkrishnanr View Post
    Can I have that code corrected ?

    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();
    	}
    }

  6. #6
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default Array index out of bound exception error

    Thanks a lot dude. That was really helpful.

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default

    Spam reported.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Array index Out of bound Exception
    By nitin_daviet88 in forum New To Java
    Replies: 9
    Last Post: 07-28-2010, 05:32 AM
  2. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-18-2010, 11:25 PM
  3. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 04:02 PM
  4. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  5. Array Index Out of bound exception
    By abhijit in forum Networking
    Replies: 7
    Last Post: 09-25-2009, 07:25 PM

Posting Permissions

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