wood burning stoves 2.0*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "array index out ofbounds exception?" Watch "array index out ofbounds exception?" New topic
Author

array index out ofbounds exception?

rafe mcD
Greenhorn

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(MoveIt2.java:66)
at sun.applet.AppletPanel.run(AppletPanel.java:380)
at java.lang.Thread.run(Thread.java:613)


/**
* Assgignment 3: MoveIt.java/html
* Programmer: Rafe McDonald
* Date: Dec 4, 2009
* Filename: MoveIt.java
* 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());



myCanvas.setBackground(Color.blue);

// 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
Greenhorn

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
Marshal

Joined: Mar 22, 2005
Posts: 42359
    
  64
I'm not getting an exception when running that code.


Ping & DNS - my free Android networking tools app
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

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

Thank you.
rafe mcD
Greenhorn

Joined: Dec 05, 2009
Posts: 3
hello

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
Marshal

Joined: Jan 10, 2002
Posts: 61448
    
  67

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
wood burning stoves
 
subject: array index out ofbounds exception?