File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes array index out ofbounds exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "array index out ofbounds exception?" Watch "array index out ofbounds exception?" New topic

array index out ofbounds exception?

rafe mcD

Joined: Dec 05, 2009
Posts: 3
hello all

i am a student completing my final assignment and i am stumped at the following error message. i have searched the internet including the sun java website but i don't know what could be causing this error in this shortened version of the applet. below is the error code that returns. i have shortened the code to try to isolate the issue, including changing the value of the keysArray[] from 5 to 6 to 10 just to be safe but i am at a loss what this error is trying to tell me. any input would be most appreciated. BTW i am not asking you to finish my homework just clarify what is happening.

thanks in advance

basic: Exception: java.lang.ArrayIndexOutOfBoundsException: 5
java.lang.ArrayIndexOutOfBoundsException: 5
at MoveIt2.init(

* Assgignment 3:
* Programmer: Rafe McDonald
* Date: Dec 4, 2009
* Filename:
* Version: 1.0
* Purpose: This is an applet that paints a graphic within a frame and prompts the user to move the image around the frame using clickable
* buttons. The applet will use the layout manager and loop structures to test for the actionListener of the appropriate buttons.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class MoveIt2 extends Applet


// instance variables made private as not too be directly accessed from other classes inadvertantly
// the window size will be set in the html document

private Image cup;
private Panel keypad;
private Button keysArray[];

public int top = 15;
public int left = 15;

public void init()


cup = getImage(getDocumentBase(), "cup.gif");
Canvas myCanvas = new Canvas();

// construct and label the buttons for input
keypad = new Panel();
keysArray = new Button[6];
keysArray[0] = new Button("north");
keysArray[1] = new Button("south");
keysArray[2] = new Button("east");
keysArray[3] = new Button("wesh");
keysArray[4] = new Button("centre");

this.setLayout(new BorderLayout());
keypad.setLayout(new BorderLayout());


// add components to keypadPanel
keypad.add(keysArray[0], BorderLayout.NORTH);
keypad.add(keysArray[1], BorderLayout.EAST);
keypad.add(keysArray[2], BorderLayout.CENTER);
keypad.add(keysArray[3], BorderLayout.WEST);
keypad.add(keysArray[4], BorderLayout.SOUTH);
// add panel and canvas to the layout
add(myCanvas, BorderLayout.NORTH);
add(keypad, BorderLayout.SOUTH);


macpro 2x2.8 quad intel, 10.5.8, ide blueJ
rafe mcD

Joined: Dec 05, 2009
Posts: 3
ok so i found the error in my full code - i had a typo that overlooked in assigning the keypad.add keyArray[] . Border .... statement - i had a 5 in the last line but now i am confused why the simpler version is still casting the same error when it does not contain the same typo.

any ideas???

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
I'm not getting an exception when running that code.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

"phi delity", please check your private messages regarding an important administrative matter.

Thank you.
rafe mcD

Joined: Dec 05, 2009
Posts: 3

thanks for the response

i wonder if any of this could have been due to system issues. during the later stages of the day during work on this code, firefox started misbehaving. specifically highlighting text and scrolling were not functioning correctly. shortly after i made the first post i did some system maintanence and got things working.

any way thanks for confirming that the shorter version of the code works as expected. a relief to know.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

"rafe mcD", please check your private messages for an important administrative matter. Again.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: array index out ofbounds exception?
It's not a secret anymore!