繁体   English   中英

开始:NetBeans IDE 8.0中的Applet未初始化错误

[英]Start: Applet not initialized error in NetBeans IDE 8.0

我在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);
    }
}

但是我收到以下错误:

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)

Start: Applet not initialized在Applet Viewer中Start: Applet not initialized

我在另一台可以正常工作的PC上尝试了相同的代码,没有任何错误。 这是任何类型的错误还是错误?

您需要实例化“ Choice然后才能向其中添加项目。

@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);
}

我不知道为什么相同的代码可以在另一台PC上工作,而不是相同的代码。 无论在何处运行它,都仍然需要首先实例化Choice。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM