The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Error writing to an array with the static modifier.

I have an array of intergers that I write data to in a for loop, they have the static modifier, so I can refer to them in another method in another class, I don't really know what the static modifier does , I just put it on because eclipse said I would be unable to refer to the array otherwise. I have an array another object as well, with the same lengt, and data is written to it in the same for loop, the only difference is that it doesn't have the static modifier. Oh yes by the way, the error that I'm getting is an array out of bounds exception

The code looks like this.

importjava.awt.Graphics;publicclassMenu{public Sprite sprite;publicUtil.GuiButton button[];String file;int number;double x, y;privatestaticint buttonX;//this static array causes the problemprivatestaticint buttonY[];publicMenu(int number, String file, double x, double y){//file which contains the images for the buttonsthis.file= file;//number of buttonsthis.number= number;//x and y position of background image for menuthis.x=(int)x;this.y=(int)y;//the array of buttons.
button =new GuiButton [number];//the intializer for the static array that causes the problem
buttonY =newint[number];}//draws the graphicspublicvoid draw(Graphics g){//draws background image.this.sprite= ImageLoader.get().getSprite(file +"\\menu.png");int width = sprite.getWidth();
sprite.Draw(g,(int) x,(int) y);for(int i =0; i < number; i++){//draws buttons equally on the screen.this.button[i]= ImageLoader.get().getGuiButton(file +"\\button"+ i +".png");double remainingSpace =541-((number -1)*(520/number));
button[i].Draw(g, ((int)x +width/2)-(button[i].getWidth()/4), ((i*(541/number))+((int)remainingSpace /2))- button[i].getHeight());//creates variables for the x and y position of the button, so I can test to see if they were clicked in a mouseListener
buttonX =((int)x +width/2)-(button[i].getWidth()/4);//the line that eclipse says the error is on when the code is run
buttonY[i]=((i*(541/number))+((int)remainingSpace /2))- button[i].getHeight();}}

Re: Error writing to an array with the static modifier.

I've worked out what was wrong, the array inilalizer was not static, but for some reason it did not put an error when the array was declared, it just created an array with the length of zero. Sorry to waste people's time, MERRY CHRISTMAS