• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array index out ofbounds exception?

 
rafe mcD
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

}
}
 
rafe mcD
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not getting an exception when running that code.
 
Paul Clapham
Sheriff
Posts: 20989
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"phi delity", please check your private messages regarding an important administrative matter.

Thank you.
 
rafe mcD
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"rafe mcD", please check your private messages for an important administrative matter. Again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic