Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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.
     
     
    import java.awt.Graphics;
     
     
     
    public class Menu{
     
    	public Sprite sprite;
    	public Util.GuiButton button[];
    	String file;
    	int number;
    	double x, y;
    	private static int buttonX;
    	//this static array causes the problem
    	private static int buttonY[];
     
     
    	public Menu(int number, String file, double x, double y){
    		//file which contains the images for the buttons
    		this.file = file;
    		//number of buttons
    		this.number = number;
    		//x and y position of background image for menu
    		this.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 = new int[number];
    	}
     
    	//draws the graphics
    	public void 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();
    			}
     
    	}
    Last edited by handuel; December 25th, 2011 at 10:51 AM.


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

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

    PS, I tried running without the static modifier and IT WORKED PERFECTLY, unfortunately the variable is then useless to me because I can't refer to it in my method.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    19
    My Mood
    Inspired
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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

Similar Threads

  1. Right way to code a static initializer with fatal error?
    By Sean4u in forum Java Theory & Questions
    Replies: 5
    Last Post: September 27th, 2011, 02:47 PM
  2. Blackberry Api: Writing an array of bytes to a file
    By jules0075 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: July 18th, 2011, 01:50 PM
  3. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  4. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM
  5. "illegal modifier for parameter sum; only final is permitted
    By etidd in forum Java Theory & Questions
    Replies: 3
    Last Post: February 11th, 2010, 07:51 AM