0

Hello to all,
I have following simple Applet program.

 import java.applet.*;
    import java.awt.*;

    public class myapp1 extends Applet
    {
    Label l1, l2, l3, l4;
    TextField t1;
    TextArea t2;
    Checkbox c1,c2,c3,c4,c5,c6;
    CheckboxGroup cg;
    Button b;
    public void init()
    {
    l1=new Label("Enter your name: ", Label.RIGHT);
    t1=new TextField(20);
    l4= new Label("Enter your gender: ");
    cg=new CheckboxGroup();
    c5=new Checkbox("Male",cg,true);
    c6=new Checkbox("Female",cg,false);
    l2=new Label ("About Workshop: ");
    c1=new Checkbox("Timely Completion");
    c2=new Checkbox("Excellent Resourse Persons", true);
    c2.setEnabled(false);
    c3=c1=new Checkbox("Good Food");
    c1=new Checkbox("Excellent Hospitality");
    l3=new Label("Any other comment:");
    t2=new TextArea(10,20);
    b=new Button("OK");
    b.setForeground(Color.blue);
    add(l1);
    add(t1);
    add(l4);
    add(c5);
    add(c6);
    add(l2);
    add(c1);
    add(c2);
    add(c3);
    add(c4);
    add(l3);
    add(t2);
    add(b);
    }
    }

This program shows blank Applet with Labels, Button and CheckBoxes.
It shows follo. exception on Command Prompt.

java.lang.NullPointerException
        at java.awt.Container.addImpl(Container.java:1086)
        at java.awt.Container.add(Container.java:410)
        at myapp1.init(myapp1.java:39)
        at sun.applet.AppletPanel.run(AppletPanel.java:434)
        at java.lang.Thread.run(Thread.java:722)

Please help me to display proper output.

2
Contributors
2
Replies
14
Views
3 Years
Discussion Span
Last Post by deolalkar_pooja
0

at myapp1.init(myapp1.java:39)

tells you its on line 39, ie add(c4); the only thing that can be null in that is c4. You declare c4 on line 9, but you never seem to give it a value, so it's null. On line 21 you give c1 a value. On line 25 you give it a different value. Should that have been c4?

0

Greate....i did so simple mistake.
Actually I didnot understood the exception meaning. Now I got clear idea about it.

Thank you.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.