Results 1 to 9 of 9
Thread: array index out of bound error
- 04-27-2012, 08:28 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
array index out of bound error
here is my main program
my window classJava Code:import java.io.*; public class T1Main { public static void main(String[] args)throws Exception { Window w=new Window(20,30,'.'); w.display(); } }
and my shape classJava 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"); } } }
i am trying to display an empty square with "." charactersJava 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 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.
- 04-27-2012, 08:44 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.)
- 04-27-2012, 08:52 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
Re: array index out of bound error
ok i did something
- 04-27-2012, 08:54 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 04-27-2012, 08:55 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 74
- Rep Power
- 0
Re: array index out of bound error
You should use [code] not [quote] ;)
- 04-27-2012, 08:58 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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
- 04-27-2012, 09:05 AM #7
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
Website: Learn Java by Examples
- 04-27-2012, 09:08 AM #8
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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
- 04-27-2012, 09:24 AM #9
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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:
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.Java Code:private int numberOfRows,numberOfColumns; ... private char win[][]=new char[numberOfRows][numberOfColumns];
Website: Learn Java by Examples
Similar Threads
-
Array Index Out of Bound
By ankiit in forum New To JavaReplies: 9Last Post: 01-04-2012, 06:03 AM -
This is a pgm for text detection.This shows array Index Out of Bound Exception..
By gopika in forum Advanced JavaReplies: 5Last Post: 11-22-2011, 12:42 PM -
Array index out of bound exception error
By rahulkrishnanr in forum Threads and SynchronizationReplies: 7Last Post: 10-12-2010, 05:57 PM -
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 bound exception
By abhijit in forum NetworkingReplies: 7Last Post: 09-25-2009, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks