Results 1 to 4 of 4
  1. #1
    JuliaDavies is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Code won't compile - ArrayIndexOutOfBoundsException

    Hello, I am trying to produce a GUI with my Sudoku Problem solver.
    However, the array index out of bounds exception keeps getting thrown and I have no idea what's wrong with it!!

    public final class SwingSudokuBoard extends SudokuBoard {
    public TextField[][] cells; // Graphical game board
    private JPanel panel = new JPanel(); // Container

    public SwingSudokuBoard(int size) {
    super(size);

    cells = new TextField[size][size];

    cells[0][0] = SwingSudokuImplement.textField0a;
    //initialisation of all cells like above



    }

    public SwingSudokuBoard(int[][] board) {
    this(board.length);
    cells = new TextField[size][size];
    for (int row = 0; row < size; row++) {
    for (int col = 0; col < size; col++) {
    setCell(board[row][col], row, col); <------THIS LINE THROWS EXCEPTION
    }
    }
    }


    @Override
    public void setCell(int num, int row, int col) {
    super.setCell(num, row, col);
    String text = (num == EMPTY) ? "" : String.valueOf(num);
    cells = new TextField[row][col];
    cells[row][col].setText(text); <------THIS LINE THROWS EXCEPTION
    }


    @Override
    public int getCell(int row, int col) {
    int cell;

    try {
    cell = Integer.parseInt(cells[row][col].getText());
    }
    catch (NumberFormatException e) {
    cell = EMPTY;
    }
    return cell;
    }


    public JPanel getPanel() {
    return panel;
    }
    }


    ///////////////////////////////////////////////////////// SudokuBoard class:

    public class SudokuBoard {
    int EMPTY = 0; // Empty cells marker
    final int size; // Size of the board (number of rows and columns)
    final int box_size; // Size of the inner boxes

    int[][] board; // 2D array representing the game board


    public SudokuBoard(int size) {
    board = new int[size][size];
    this.size = size;
    this.box_size = (int) Math.sqrt(size);
    }

    public SudokuBoard(int[][] board) {
    this(board.length);
    this.board = board;
    }


    public void setCell(int num, int row, int col) {
    board[row][col] = num;
    }

    public int getCell(int row, int col) {
    return board[row][col];
    }
    }

    please can anyone help me? its urgent!! THANK YOU!!!!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Code won't compile - ArrayIndexOutOfBoundsException

    Please clarify your problem greatly. You state that the code won't compile, and then state that an exception is thrown -- but this state of affairs is impossible to occur. If the code won't compile, then it can't be run, and run-time exceptions can never be thrown.

    Show the actual exception text please, indicate which line throws it, and please check out the forum faq and my links below to see how to use code tags so that your posted code is readable.

    good luck!

  3. #3
    JuliaDavies is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default Re: Code won't compile - ArrayIndexOutOfBoundsException

    Sorry, I didn't mean won't compile, I meant I doesn't run!

    run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Sudoku.SwingSudokuBoard.setCell(SwingSudokuBoard.j ava:156) <---- This is the cells[row][col].setText(text);
    at Sudoku.SwingSudokuBoard.<init>(SwingSudokuBoard.ja va:140) <---- This is the setCell(board[row][col], row, col);
    at Sudoku.MainClass.main(MainClass.java:28)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)

    Thank you! sorry for the confusion

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Code won't compile - ArrayIndexOutOfBoundsException

    You've already been told to not double post.
    HELP!! NullPointerException - code won't compile

    If you continue to add new threads for the same question, you may face a ban.

    db

    THREAD CLOSED

    Live thread: HELP!! Code won't compile - NullPointerException
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. I cant get my code to compile Need help please
    By MikeJ39 in forum New To Java
    Replies: 6
    Last Post: 10-15-2011, 06:28 AM
  2. Can not compile code , anyone help me fix it ?
    By batista11b5 in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 02:00 PM
  3. Java Code Won't Compile
    By JavaStudent1990 in forum New To Java
    Replies: 4
    Last Post: 07-29-2010, 09:34 AM
  4. Code does not Compile
    By ustar in forum New To Java
    Replies: 13
    Last Post: 03-06-2010, 04:54 PM
  5. Code will not compile
    By ShotGunRockets in forum New To Java
    Replies: 17
    Last Post: 05-10-2009, 03:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •