Results 1 to 7 of 7
  1. #1
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Unhappy 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.*;
    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]);
    	}
    }
    Also, could there be something wrong with my html?

    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>

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Mean about this,

    Java Code:
    			pageTitle[6] = "Albino Blacksheep";
    			pageLink[6] = getURL("http://www.albinoblacksheep.com");
    with this,

    Java Code:
    String pageTitle[] = new String[6];
    		URL pageLink[] = new URL[6];

  4. #4
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Smile 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.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  6. #6
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default 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?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:29 AM
  2. java.lang.ArrayIndexOutOfBoundsException
    By mensa in forum Java 2D
    Replies: 7
    Last Post: 05-05-2008, 09:09 AM
  3. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 09:38 AM
  4. java.lang.ArrayIndexOutOfBoundsException
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-02-2007, 09:40 PM
  5. java.lang.ArrayIndexOutOfBoundsException
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 05:15 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •