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

Thread: Help! ArrayIndexOutOfBoundsException

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help! ArrayIndexOutOfBoundsException

    Hello, I have to make a sudoku-like GUI puzzle for a program. I was originally going to do this by using a GridLayout but decided it was easier toedit the JTextFields by making a 2D array of JTextFields. However, I am getting an ArrayIndexOutOfBoundsException and I have no idea why. Can someone please help?

    This is the section of code that is getting the error:

    private Component getPuzzle() {

    JPanel puzzlePanel = new JPanel();
    int col = 16;
    int row = 16;
    JTextField sudoku[][] = new JTextField[col][row];
    //puzzlePanel.setLayout(new GridLayout(col,row,1,1));
    puzzlePanel.add(sudoku[col][row]);

    for (int i =0; i < row*col; i++) {
    puzzlePanel.add(new JTextField());
    }

    return puzzlePanel;
    }

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    What is your actual error? What line is it on?

    Hint: When do you initialize your the values contained by your array?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    Also if anyone could suggest a way to compare values in the rows and columns in the array. Is it possible to use the compareTo() method to compare the Strings in the text fields? For example compare all the values in [1][0],[2][0],[0][1],[0][2] ect to make sure 2 of the same values aren't used twice?

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
    at sudoku.SudokuBoard.getPuzzle(SudokuBoard.java:84)

    line 84 is: puzzlePanel.add(sudoku[col][row]);

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help! ArrayIndexOutOfBoundsException


  6. #6
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    I changed JTextField sudoku[][] = new JTextField[col][row]; to JTextField sudoku[][] = new JTextField[20][20]; and that got rid of the out of bounds exception but now there is a null pointer exception

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at sudoku.SudokuBoard.getPuzzle(SudokuBoard.java:83)

    I'm thinking about just going back to the GridLayout because I had that atleast displaying what I needed but is there a way to compare JTextFields in a GridLayout and add a 2D String array to the layout?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    There is a variable on line 83 that has a null value. Look at that line, find the variable and backtrack through your code to find out why and change it to give the variable a valid value.
    is there a way to compare JTextFields
    How do you mean compare? Do you mean the contents of the text fields?

    In what kind of a component do you want to display the 2D String array?

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    Quote Originally Posted by Norm View Post
    How do you mean compare? Do you mean the contents of the text fields?

    In what kind of a component do you want to display the 2D String array?

    I'm creating a hexadecimal sudoku puzzle so I need to compare the strings 0-9 and a-f to make sure there are no repeats. And I was going to display the 2D String array in a grid of JTextFields each holding 1 value

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    Then you mean you want to compare the contents of two text fields? Is that right?

  10. #10
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    Well it is a 16 x 16 grid so I would need to compare 16 text fields

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help! ArrayIndexOutOfBoundsException

    What is it that you want to detect by comparing the text fields?
    For example: Do any two textfields have the same value?

    If all the textfields are in an array, then going thru the array should be easy.

    16 x 16 grid so I would need to compare 16 text fields
    Please explain this. 16x16 = 256

Similar Threads

  1. [SOLVED] java.lang.ArrayIndexOutOfBoundsException
    By anmaston in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2011, 04:43 PM
  2. [SOLVED] ArrayIndexOutOfBoundsException
    By Elementality in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 18th, 2011, 07:39 PM
  3. ArrayIndexOutOfBoundsException
    By NightFire91 in forum Exceptions
    Replies: 1
    Last Post: October 31st, 2010, 06:06 AM