Results 1 to 6 of 6
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default ArrayOutofBounds Exception

    Alright I'm stumped, I'm trying to assign an int variable as the amount of array cells in an array. However, no matter how I assign the value the array takes a 0 as number of array cells and then gives an out of bounds exception. I think it has something to do with the array already setting its value before the variable is set to the intended value (I hope that makes sense).

    here's the code:

    main execution class:
    Java Code:
    //Create a new cells
    		Cell test = new Cell ();
    		Cell test1 = new Cell ();
    		
    		//Configure the cells
    		test.ConfigureCell(20);
    		test1.ConfigureCell(20);
    cell class:

    Java Code:
    private int wLength;
    	
    	//Add array for the words that the cell will contain
    	private String words[] = new String [wLength];
    ...
    
    public void ConfigureCell (int i){
    		number_of_cells += 1;
    		cell_fileName = "Cell_" + number_of_cells + ".txt";
    		i = wLength;
    	}
    I'm still learning java so sorry if this is something fundamental that I'm missing.

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

    Default

    gives an out of bounds exception.
    Please post the full text of the error message.
    What line does the error occur on? What is the value of the index? What is the size of the array?

    The only array I see is:

    Java Code:
    private String words[] = new String [wLength];
    What is the value of wLength when the array is created?

  3. #3
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    Error:

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    	at mainPackage.Cell.SetValues(Cell.java:24)
    	at mainPackage.ExecuteBrainstorm.main(ExecuteBrainstorm.java:41)
    The value of wLength when it is set is 0. Even though I used the method configureCell to set it to 20, that's my problem. I understand why there's an array out of bounds exception but I don't understand why my value which I pass to wLength is not being used to initialize the array.

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

    Default

    The value of wLength when it is set is 0.
    Then the array has no elements and an index of 0 (the first element) would be invalid.
    You should wait until you know how many elements you need in the array before creating it.

    I don't understand why my value which I pass to wLength is not being used to initialize the array.
    When the array was created, it used the value in wLength at that time. Changing the value of wLength later will not go back and redo any previous uses of wLength.

  5. #5
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default

    so then a solution would be to create a constructor and set wLength then?

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

    Default

    set wLength then?
    It needs to be set before it is used to create the array.
    Create the array in the constructor when its desired size is known.

Similar Threads

  1. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 04:21 AM
  2. Replies: 5
    Last Post: 02-15-2011, 12:39 AM
  3. Replies: 1
    Last Post: 02-06-2011, 06:38 AM
  4. Replies: 4
    Last Post: 10-25-2010, 08:42 PM
  5. Replies: 1
    Last Post: 03-08-2008, 07:41 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
  •