views:

173

answers:

1

I have an array of JPanels and I'm trying to add a mouseadapter to each one so it will be possible to identify which one was clicked and then change its background colour. Everything seems to work fine except when I run it from eclipse when a page will appear that says EventDispatchThread.run() line: not available, Source not found, and in the debug window it says:

  Thread [AWT-Shutdown] (Running) 
  Daemon Thread [AWT-Windows] (Running) 
  Thread [AWT-EventQueue-0] (Suspended (exception ArrayIndexOutOfBoundsException)) 
   EventDispatchThread.run() line: not available 
  Thread [DestroyJavaVM] (Running) 
  Thread [AWT-EventQueue-0] (Running) 

This is the code:

private void drawBoard() {
  LayoutManager layout = new GridLayout(NUMBER_OF_ROWS, NUMBER_OF_COLS);
  boardPanel.setLayout(layout);
  boardPanel.setPreferredSize(new Dimension(200, 400));
  chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];
  MoveArrays move = new MoveArrays();
  move.initialisePieceMoves();
  for (int i = 0; i < NUMBER_OF_ROWS; i++) {
   for (int j = 0; j < NUMBER_OF_COLS; j++) {
    int index = i * 4 + j;
    chessBoard[i][j] = new JPanel();
    chessBoard[i][j].addMouseListener(clickSquare(j, i, index, move));
    chessBoard[i][j].setBackground(getColor(i,j));
    if (!(boardArray.chessBoard[index].square.isEmpty())) {
     Piece piece = (Piece) boardArray.chessBoard[index].square.firstElement();
     JLabel pieceString = new JLabel(piece.toString()); 
     chessBoard[i][j].add(pieceString);
    }
    boardPanel.add(chessBoard[i][j]);
   }
  }
 } // drawBoard()

 private MouseAdapter clickSquare(final int xCo, final int yCo, final int index, final MoveArrays move) {
  return new MouseAdapter() {
   public void mousePressed(MouseEvent me) {
    resetColors();
    JPanel selectedSquare = (JPanel) me.getSource();
    selectedSquare.setBackground(selectedColor());
    System.out.println("xCo: " + xCo + " yCo: " + yCo);
    Vector validMoves = move.DroneMovesNorth[index].Moves;
    int totalMoves = move.DroneTotalMovesNorth[index];
    if (!validMoves.isEmpty()) {
     for (int n = 0; n <= totalMoves; n++) {
      String stringMove = validMoves.elementAt(n).toString();
      int intMove = Integer.parseInt(stringMove);
      System.out.println("intMove: " + intMove);
     }
    }
   }
  };
 }

I think it might be the fact that I cast the me.getSource to a JPanel but shouldn't it be one anyway? If I don't put the cast it says that it cannot bind an 'Object' to a JPanel, when I do system.out.print(me.getSource()) it prints a line saying that it's a JPanel so I don't get what the problem is T_T Any help would be muchhhh appreciated!

+4  A: 

It appears the problem is not to do with the source of the mouse event. Indeed I think there's two different things being called "source" here, and you're mixing them together. I think the message:

EventDispatchThread.run() line: not available, Source not found

Is Eclipse telling you that the library does not have the source code attached to it, so it cannot find line numbers or show you the source code. The problem does not appear to be with me.getSource().

The problem is that you are trying to reference an index within an array that is outwith the bounds of the array (hence the ArrayIndexOutOfBoundsException in the stack trace).

Since the stack trace is on the AWT Event Queue thread, it is likely the exception is stemming from within the mousePressed() method of your MouseAdapter. Since you're already using Eclipse, I suggest you get to know the Debugger, which is extremely useful. Put a breakpoint at these lines:

Vector validMoves = move.DroneMovesNorth[index].Moves;
int totalMoves = move.DroneTotalMovesNorth[index];

And check that both the array fields of move are large enough to reference an element at the index. If you don't want to use the debugger (I'd really recommend that way), then you could wrap those two lines with an exception catch, like this:

try {
    Vector validMoves = move.DroneMovesNorth[index].Moves;
    int totalMoves = move.DroneTotalMovesNorth[index];
catch(ArrayIndexOutOfBoundsException e) {
    System.out.println("Exception thrown: index = " + index + 
        "Array lengths: " + move.DroneMovesNorth.length + ", " +
        move.DroneTotalMovesNorth.length);
}

When/if the exception is caught, you want to find out why the index is larger than the size of each of these arrays. That's left as an exercise ;-)


Edit: there's some other code in there which looks suspicious.

First you declare the array for the chessboard:

chessBoard = new JPanel[NUMBER_OF_ROWS][NUMBER_OF_COLS];

Then you begin to iterate for every square in the board:

for (int i = 0; i < NUMBER_OF_ROWS; i++) {
   for (int j = 0; j < NUMBER_OF_COLS; j++) {

Note that i will run up to the value NUMBER_OF_ROWS, which is the length of the chessboard array. Then for some reason I don't understand, you change the value of index to something that could be 4x as large as the length of the array:

int index = i * 4 + j;

And later try to reference that position in the chessboard array:

if (!(boardArray.chessBoard[index].square.isEmpty())) {

This means it's possible for index to be a value higher than NUMBER_OF_ROWS, and if you try to access the element at that index of the array, it will throw the ArrayIndexOutOfBoundsException.

So, my second suggestion would be to take another look at the logic involved in:

int index = i * 4 + j;

... as that may also be the problem.

P.S. the debugger in Eclipse is awesome, you should use it ;-)

Grundlefleck
Sorry if you find this to be nitpicking, you wrote:"you change the value of i to something that could be 4x", this should be "you change the value of 'index' to something that could be 4x as large"
sateesh
Yes, I do think it's nitpicking, but it's the *good kind*, no need to apologise! Thanks sateesh :-)
Grundlefleck
ohh the boardArray class is just a regular 1d array lol I was trying to convert the 2d index into a 1d one, i wanted to separate the gui stuff from the logic and ai stuff so I have a console version of the game aswell. Thanks for the answer though :D I'll go try what you said now
Becky
okay I figured it out lol, such a stupid mistake but my for loop was iterating 3 times when the length of the array was only 2 because the conditional was "n <= totalMoves" instead of "n < totalMoves"
Becky
Heh, well I never noticed that one. Glad you're sorted now though. Out of interest, did you use the debugger to help you figure that out, or was it just reasoning through the code?
Grundlefleck
I did use the debugger and noticed all the errors were coming up on this line "int totalMoves = move.DroneTotalMovesNorth[index];" which is the variable used in the for loop. Also, I set the default JRE to the JDK and that got rid of the "source not found" thing and it now shows the EventDispatch source code which says that Event Dispatch thread dies in case of an uncaught exception.
Becky