1

I am using a applet to create a GUI but whenever I am running the code it is showing "Applet not initialized". Error: load: home_1.class can't be instantiated.

The error is

 java.lang.InstantiationException
        at sun.reflect.InstantiationExceptionConstructorAccessorImpl.
newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at sun.applet.AppletPanel.createApplet(Unknown Source)
        at sun.applet.AppletPanel.runLoader(Unknown Source)
        at sun.applet.AppletPanel.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)

The code is...

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public abstract class home_1 extends Applet implements ItemListener 
{
    Checkbox EPL,LALIGA,BUNDESH;
    CheckboxGroup menu;
    String msg="";
    public void init()
    {
        menu = new CheckboxGroup();
        EPL = new Checkbox("English Premier League",menu,true);
        LALIGA = new Checkbox("La Liga",menu,false);
        add(EPL);
        add(LALIGA);
        EPL.addItemListener(this);
        LALIGA.addItemListener(this);
    }
    public void itemStateChanged(ItemEvent ie)
    {
        repaint();
    }
    public void paint(Graphics g)
    {
        msg = "Current Selection: ";
        msg+=menu.getSelectedCheckbox().getLabel();
        g.drawString(msg,6,6);
    }

}
Pritam
  • 31
  • 8
  • Why AWT? Why applets at all? – MadProgrammer Apr 11 '14 at 03:38
  • 1
    I echo the concern of @MadProgrammer (except longer) 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Apr 11 '14 at 05:08

1 Answers1

2

public abstract class home_1 and you're surprised it doesn't work?

abstract classes can't be instantiated...

Also, you must call super.paint(g) as the first line of your paint method.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366