I wrote this program, when i run it, everything seems to work ok, but in the eclipse console i get a long list of errors, what is wrong with my program and how should i fix it?
thanks

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

// <applet code="AppletInterfaz" width="300" height="200"> </applet>

public class AppletInterfaz extends Applet implements ItemListener {
    List colors;

    public AppletInterfaz() {
        colors = new List(4);
        colors.add("Aqua");
        colors.add("Black");
        colors.add("Blue");
        colors.add("Fuchsia");
        colors.add("Gray");
        colors.add("Green");
        colors.add("Lime");
        colors.add("Maroon");
        colors.add("Navy");
        colors.add("Olive");
        colors.add("Purple");
        colors.add("Red");
        colors.add("Silver");
        colors.add("Teal");
        colors.add("White");
        colors.add("Yellow");
        add(colors);
        colors.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie) {
        repaint();
    }

    public void paint(Graphics g) {
        int choice = colors.getSelectedIndex();
        int red, green, blue;
        int colorMix[][] = { { 0, 255, 255 }, { 0, 0, 0 }, { 0, 0, 255 },
                { 255, 0, 255 }, { 128, 128, 128 }, { 0, 128, 0 },
                { 0, 255, 0 }, { 128, 0, 0 }, { 0, 0, 128 }, { 128, 128, 0 },
                { 128, 0, 128 }, { 255, 0, 0 }, { 192, 192, 192 },
                { 0, 128, 128 }, { 255, 255, 255 }, { 255, 255, 0 } };

        red = colorMix[choice][0];
        green = colorMix[choice][1];
        blue = colorMix[choice][2];
        Color fondo = new Color(red, green, blue);
        g.setColor(fondo);
        g.drawRect(0, 0, 300, 200);
        g.fillRect(0, 0, 300, 200);
    }

}

this is the list of errors

Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: -1
    at AppletInterfaz.paint(AppletInterfaz.java:48)
    at sun.awt.RepaintArea.paintComponent(Unknown Source)
    at sun.awt.X11.XRepaintArea.paintComponent(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.X11.XComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Recommended Answers

All 3 Replies

Thta's just one error, with a complete list of where it came from. In theis case the first two lines tell you everything - on line 48 you try to access an array element [-1]
Almost certainly because paint is being called before you select something in "colors", ie selected index == -1

Try this

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.List;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
// <applet code="AppletInterfaz" width="300" height="200"> </applet>
public class AppletInterfaz extends Applet implements ItemListener {
    List colors;
    public AppletInterfaz() {
        colors = new List(4);
        colors.add("Aqua");
        colors.add("Black");
        colors.add("Blue");
        colors.add("Fuchsia");
        colors.add("Gray");
        colors.add("Green");
        colors.add("Lime");
        colors.add("Maroon");
        colors.add("Navy");
        colors.add("Olive");
        colors.add("Purple");
        colors.add("Red");
        colors.add("Silver");
        colors.add("Teal");
        colors.add("White");
        colors.add("Yellow");
        add(colors);
        //colors.setSelectedIndex(0);
        colors.addItemListener(this);
    }
    public void itemStateChanged(ItemEvent ie) {
        repaint();
    }
    public void paint(Graphics g) {
        //colors.setSelectedIndex(colors.getSelectedIndex());
        int choice;
if (colors.getSelectedIndex()<= -1 )
{
    choice = colors.getSelectedIndex()+1;
}
else
{
    choice = colors.getSelectedIndex();
}
        System.out.println(choice);
        int red, green, blue;
        int colorMix[][] = { { 0, 255, 255 }, { 0, 0, 0 }, { 0, 0, 255 },
                { 255, 0, 255 }, { 128, 128, 128 }, { 0, 128, 0 },
                { 0, 255, 0 }, { 128, 0, 0 }, { 0, 0, 128 }, { 128, 128, 0 },
                { 128, 0, 128 }, { 255, 0, 0 }, { 192, 192, 192 },
                { 0, 128, 128 }, { 255, 255, 255 }, { 255, 255, 0 } };
        red = colorMix[choice][0];
        green = colorMix[choice][1];
        blue = colorMix[choice][2];
        Color fondo = new Color(red, green, blue);
        g.setColor(fondo);
        g.drawRect(0, 0, 300, 200);
        g.fillRect(0, 0, 300, 200);
    }
}

Actually ur choice variable gets value as -1 so it shows the error of OutofIndex....since no item is selected in the List...
bydefault select some item in the List and then assign the value to choice variable....

commented: thanks for the help +0

thanks for explaining and helping me to solve the problem :)
now it works without errors.

public void paint(Graphics g) {
        int choice = colors.getSelectedIndex();
        if (choice == -1) {
            choice = 10;
        }

i chosed 10 'cause i like the color :)
thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.