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 2 of 2

Thread: What did I do wrong here?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post What did I do wrong here?

    Well I'm starting to study a bit on Swing and the JFrame, so please, if this looks ugly please cut me a bit of slack >.<
    This is what I've got:

    package Core.Engine;
     
    import javax.swing.*;
    import java.awt.*;
     
     
     
    @SuppressWarnings("serial")
    public class Engine extends JFrame {
     
     
     
    static int i;
    	static JPanel PRIME;
    	public Engine(){
     
    		JButton[] BUTTON = new JButton[9];
    		PRIME = new JPanel(new GridLayout(5,6));
    		String b[] = {"1", "2","4", "3", "5",
    					"6", "7", "8", "9", "0"};
     
    		for(i = 0; i < BUTTON.length; i++){
    			BUTTON[i] = new JButton(b[i]);
    			BUTTON[i].setSize(30,30);
    			};
     
    		PRIME.add(BUTTON[i]);
    	}
    	public static void main(String args[]){
    		Engine frame = new Engine();
    		frame.add(PRIME);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }

    I get an ArrayIndexOutOfBoundsException on line 27 and 30 which would be in the Main argument...
    Here's the exact error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
    	at Core.Engine.Engine.<init>(Engine.java:27)
    	at Core.Engine.Engine.main(Engine.java:30)

    Could anyone tell me where I went wrong? The array has 9 elements. I set 9 chars... How did it go out of bounds?



    Foot Note:
    If you have any ideas on how I could improve this code please don't be afraid to mention it!
    Last edited by Paetilium; March 19th, 2012 at 10:54 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: What did I do wrong here?

    I am guessing line 27 is this one:

    PRIME.add(BUTTON[i]);

    You get an ArrayIndexOutOfBoundsException when you try and access an array with a negative index, or one >= the length of the array. Remember that array indices start at zero and go to length-1. You can test whether you are doing this here by inserting a System.out.println():

    System.out.println("BUTTON has length " + BUTTON.length);
    System.out.println("About to access BUTTON[i] where i=" + i);
    PRIME.add(BUTTON[i]);

    -----

    It would be good if you followed Java coding conventions and used lowercase letters for packages and variables. This adds readability.

    In terms of the logic of your code it you should remove any use of "static" other than for main(). And declare variables close to where you use them. In this case the int i might better be declared as part of the for loop rather than as a static class variable. (Of course both of these things may involve addressing problems to which the compiler alerts you. But, often, problems are made worse, not better, if the compiler is made to shut up by making variables static and/or giving them excessive scope.)

Similar Threads

  1. Idk what is wrong
    By Devourz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2011, 05:05 AM
  2. WHAT IS WRONG HERE PLEASE?
    By mjava in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 27th, 2010, 08:29 PM
  3. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 02:23 PM
  4. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM
  5. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM