Results 1 to 9 of 9
  1. #1
    anya is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Question array index out of bound error

    here is my main program



    Java Code:
    import java.io.*;
    
    
    public class T1Main
    
    {
       public static void main(String[] args)throws Exception
       
    {
          Window w=new Window(20,30,'.');
    
        
    
    
         
     w.display();
       
    }
    }
    my window class

    Java Code:
    import java.io.*;
    
    
    public class Window
    
    {
       private int numberOfRows,numberOfColumns;
       
    private char borderCharacter;
       
    private char win[][]=new char[numberOfRows][numberOfColumns];
    
      
     public Window(int numberOfRows,int numberOfColumns,char borderCharacter)
      
     {
          this.numberOfRows=numberOfRows;
          
    this.numberOfColumns=numberOfColumns;
         
     this.borderCharacter=borderCharacter;
       }
    
      
     public void addShape(Shape shape)
       
    {
          shape.draw(this);
       }
    
      
     public void removeShape(Shape shape)
       
    {
       }
    
       
    void display()
       
    {
    
        for(int i=0;i<numberOfRows;i++)
          
     {
           for(int j=0;j<numberOfColumns;j++)
           
     {
               if((i==0)||(i==(numberOfRows-1))||(j==0)||(j==(numberOfColumns-1)))
               
     {
                   win[i][j]=borderCharacter; //throwing error message on this line
                  
     System.out.print(borderCharacter);
                }
              
      else
               
     {
                   System.out.print(" ");
                }
    
      
     
              }
             
     System.out.println("\n");
    
       }
       }
    }
    and my shape class


    Java Code:
    public abstract class Shape
    
    {
       public  int rowBase,colBase;
    
      
     public Shape(int rowBase,int colBase)
       
    {
          this.rowBase=rowBase;
          
    this.colBase=colBase;
       }
    
    
    public int getRowBase()
    
    {
       return rowBase;
    }
    
    
    public int getColBase()
    
    {
       return colBase;
    }
    
    
    public abstract void draw(Window window);
    }
    i am trying to display an empty square with "." characters

    i am getting a runtime error "array index out of bound"can u please point out my mistake.
    Last edited by anya; 04-27-2012 at 08:56 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: array index out of bound error

    Could you please edit your post to include code formatting? Also it would be a good idea to post the entire runtime error message and stack trace (the thing that says which lines of which Java file were involved in the exception.)

  3. #3
    anya is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: array index out of bound error

    ok i did something

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: array index out of bound error

    Array index out of bound exception generally means that you are trying to access an array using an illegal index. This index can be either negative value or a value that greater than or equal to the size of the array. So please check if you make any illegal access to that array.

  5. #5
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default Re: array index out of bound error

    You should use [code] not [quote] ;)

  6. #6
    anya is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: array index out of bound error

    i know what the error means,i checked my program,according to me it should display what i want

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: array index out of bound error

    Alright, so you know what it means. Now, as pbrockway2 ask you before, do you have the error stack trace? You have to start looking there to find which line started the exception. Then find which part of your code access an array using an illegal index. Print out the variable the hold the indexes will help you.

  8. #8
    anya is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: array index out of bound error

    there is no stack,i pointed out the line where the error is thrown.it just tells me at that line aRRayindexoutofbound

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: array index out of bound error

    There should be a stack trace for that error.

    Anyway, let see what you have in your code. In your code you create the array like this:

    Java Code:
    private int numberOfRows,numberOfColumns;
    ...    
    private char win[][]=new char[numberOfRows][numberOfColumns];
    At this point the numberOfRows and numberOfColumns will have a 0 value. And that cause your win array creation become new char[0][0]. You have a zero sized array there. So when you try to access it you'll get the exception.

Similar Threads

  1. Array Index Out of Bound
    By ankiit in forum New To Java
    Replies: 9
    Last Post: 01-04-2012, 06:03 AM
  2. Replies: 5
    Last Post: 11-22-2011, 12:42 PM
  3. Array index out of bound exception error
    By rahulkrishnanr in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-12-2010, 05:57 PM
  4. Array index Out of bound Exception
    By nitin_daviet88 in forum New To Java
    Replies: 9
    Last Post: 07-28-2010, 05:32 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
  •