MrShadow
MrShadow

Reputation: 171

Start: Applet not initialized error in NetBeans IDE 8.0

I was trying the following code on NetBeans IDE 8.0:

public class ChoiceProgramDemo extends Applet implements ItemListener{

    Label l1 = new Label();
    Choice Product;

    @Override
    public void init() {
        String[] ProductList = new String[4];
        ProductList[0]="Pen";
        ProductList[1]="Pencil";
        ProductList[2]="Eraser";
        ProductList[3]="NoteBook";

        for(int i=0;i<ProductList.length;i++)
        {
            Product.insert(ProductList[i], i);
        }

        add(Product);
        add(l1);
        Product.addItemListener(this);
    }
    public void itemStateChanged(ItemEvent ie)
    {
        int Selection;
        Selection=Product.getSelectedIndex();
        System.out.println(Selection);
    }
}

But I am getting the following error:

java.lang.NullPointerException
    at ChoiceProgramDemo.init(ChoiceProgramDemo.java:35)
    at sun.applet.AppletPanel.run(AppletPanel.java:435)
    at java.lang.Thread.run(Thread.java:745)

and Start: Applet not initialized in the Applet Viewer.

I tried the same code on another PC on which it worked fine without any error. Is this any type of bug or an error?

Upvotes: 0

Views: 2349

Answers (1)

Paul
Paul

Reputation: 20091

You need to instantiate the Choice before adding items to it.

@Override
public void init() {
    // you are missing this line
    Choice Product = new Choice();
    //
    String[] ProductList = new String[4];
    ProductList[0]="Pen";
    ProductList[1]="Pencil";
    ProductList[2]="Eraser";
    ProductList[3]="NoteBook";

    for(int i=0;i<ProductList.length;i++)
    {
        Product.insert(ProductList[i], i);
    }

    add(Product);
    add(l1);
    Product.addItemListener(this);
}

I don't know why the same code would work on another PC other than it wasn't the same code. No matter where you run it, you still need to instantiate the Choice first.

Upvotes: 1

Related Questions