Results 1 to 7 of 7
- 03-02-2010, 03:06 AM #1
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
java.lang.ArrayIndexOutOfBoundsException: 6
While trying to learn how to make applets I tried to make a homepage for myself with revolving links.
It compiled just fine but when I try to open it in firefox or appletviewer it gives me the error:
java.lang.ArrayIndexOutOfBoundsException: 6
This is my code.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
Also, could there be something wrong with my html?Java Code:public class Revolve extends JApplet implements Runnable, ActionListener { String pageTitle[] = new String[6]; URL pageLink[] = new URL[6]; int current = 0; Thread runner; public void init() { Color background = new Color(255,255,204); setBackground(background); pageTitle[0] = "Facebook"; pageLink[0] = getURL("http://www.facebook.com"); pageTitle[1] = "Hotmail"; pageLink[1] = getURL("http://www.hotmail.com"); pageTitle[2] = "Youtube.com"; pageLink[2] = getURL("http://www.youtube.com"); pageTitle[3] = "Google"; pageLink[3] = getURL("http://www.google.com"); pageTitle[4] = "My Life Is Average"; pageLink[4] = getURL("http://www.mylifeisaverage.com"); pageTitle[5] = "I Can Has Cheezeburger"; pageLink[5] = getURL("http://www.icanhascheezeburger.com"); pageTitle[6] = "Albino Blacksheep"; pageLink[6] = getURL("http://www.albinoblacksheep.com"); Button goButton = new Button("Go"); goButton.addActionListener(this); FlowLayout flow = new FlowLayout(); Container pane = getContentPane(); pane.setLayout(flow); pane.add(goButton); setContentPane(pane); } URL getURL(String urlText) { URL pageURL = null; try { pageURL = new URL(getDocumentBase(), urlText); } catch (MalformedURLException m) { } return pageURL; } public void paint(Graphics screen) { super.paint(screen); Graphics2D screen2D = (Graphics2D) screen; screen2D.drawString(pageTitle[current], 5, 60); screen2D.drawString("" + pageLink[current], 5, 80); } public void start() { if (runner == null) { runner = new Thread(this); runner.start(); } } public void run() { Thread thisThread = Thread.currentThread(); while (runner == thisThread) { repaint(); current++; if (current > 5) current = 0; try { Thread.sleep(10000); } catch (InterruptedException e) { } } } public void stop() { if (runner != null) { runner = null; } } public void actionPerformed(ActionEvent evt) { if (runner != null) { runner = null; } AppletContext browser = getAppletContext(); if (pageLink[current] != null) browser.showDocument(pageLink[current]); } }
XML Code:<html> <head> <title>Paul's Home Page</title> </head> <body bgcolor="#C4C4C4"> <font face="Arial" size=3> <table> <tr> <td bgcolor="#FFCCFF" width=300 valign="TOP" align="CENTER"> <h2>Paul's Home Page</h2> <p>Welcome! This page is under construction. </td> <td bgcolor="#FFFFCC" width=200 valigh="TOP" align="RIGHT"> <i><b>Some of my favorite links:</b></i> <applet code="Revolve.class" height=100 width=200> </applet> <center> <i>Click to visit</i> </center> </td> </tr> </table> </font> </body> </html>
-
Arrays are 0-based, meaning that an array that has length of 6 goes from 0 to 5.
- 03-02-2010, 11:04 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Mean about this,
with this,Java Code:pageTitle[6] = "Albino Blacksheep"; pageLink[6] = getURL("http://www.albinoblacksheep.com");
Java Code:String pageTitle[] = new String[6]; URL pageLink[] = new URL[6];
- 03-02-2010, 12:20 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
Thank You
I was really confused because it did not catch this error while compiling normally.
So basically I need to add one to when I declare the arrays.
Thank you for your help.
- 03-03-2010, 04:27 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In errors/exceptions basically you can found two categories, one is the compile time errors and the other one is the runtime errors. So this is a runtime error, because how can compiler knows how many items you are inserting. It's really happening when you are running the code.
- 03-05-2010, 02:58 PM #6
Member
- Join Date
- Feb 2010
- Posts
- 15
- Rep Power
- 0
Background Color
I'm having another problem with the same code. The background color isn't changing when I change Color background = new Color(225, 0 ,0)
I want to change it to red.
Do I have to change something in the paint() method?
- 03-06-2010, 01:12 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In simple word you have to repaint the UI again.
Similar Threads
-
java.lang.ArrayIndexOutOfBoundsException
By niteshwar.bhardwaj in forum Java 2DReplies: 0Last Post: 02-13-2009, 07:29 AM -
java.lang.ArrayIndexOutOfBoundsException
By mensa in forum Java 2DReplies: 7Last Post: 05-05-2008, 09:09 AM -
java.lang.ArrayIndexOutOfBoundsException
By riccian in forum New To JavaReplies: 0Last Post: 03-18-2008, 09:38 AM -
java.lang.ArrayIndexOutOfBoundsException
By mew in forum New To JavaReplies: 2Last Post: 12-02-2007, 09:40 PM -
java.lang.ArrayIndexOutOfBoundsException
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 05:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks