Results 1 to 4 of 4
  1. #1
    feniger is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Post OutOfBoundsException

    here's a question:
    in one class I have the method:

    static public StartBoard[] finalBoard()
    {
    StartBoard[] temp = new StartBoard[32];
    .
    .
    .
    return temp;
    }

    and in another class, inside the main method, I wrote:

    StartBoard[] try2 = new StartBoard[32];
    try2 = StartBoard.finalBoard();
    for (int i = 0; i<32; i++)
    System.out.println(try2[i].getType());

    and I got the following runtime error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
    at chess.StartBoard.finalBoard(StartBoard.java:74)
    at chess.Play.main(Play.java:21)

    I remember learning something about "contracts" with variables to be instance of a certain non-primitive type, is that the solution? and if so, how do I use it?
    thanks a head

    feniger
    have a good one - Day I mean...

  2. #2
    feniger is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    maybe its important to know to solve:
    the first code snippet is actually inside:
    public enum StartBoard {... }
    have a good one - Day I mean...

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 32
    at chess.StartBoard.finalBoard(StartBoard.java:74)
    at chess.Play.main(Play.java:21)
    Looks like you went past the end of the array, viz, element index 31, on
    line 74 in your code.
    Java Code:
    // These
    //StartBoard[] try2 = new StartBoard[32];
    //try2 = StartBoard.finalBoard();      // line 21 here?
    // can be written
    StartBoard[] try2 = StartBoard.finalBoard();
    // This looks okay.
    for (int i = 0; i<32; i++)
        System.out.println(try2[i].getType());
    ...
    public enum StartBoard {
        static public StartBoard[] finalBoard()
        {
            StartBoard[] temp = new StartBoard[32];
              .
              .
             // line 74 must be in here...
              .
            return temp;
        }

  4. #4
    feniger is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default thanks,

    yea that was it.... I'm such a newbie
    have a good one - Day I mean...

Posting Permissions

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