gpt4 book ai didi

java - 开始 : Applet not initialized error in NetBeans IDE 8. 0

转载 作者:行者123 更新时间:2023-12-01 12:47:38 25 4
gpt4 key购买 nike

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

和小程序查看器中的启动:小程序未初始化

我在另一台电脑上尝试了相同的代码,它运行良好,没有任何错误。这是任何类型的错误还是错误?

最佳答案

在向其中添加项目之前,您需要实例化 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);
}

我不知道为什么相同的代码可以在另一台电脑上运行,除非它不是相同的代码。无论您在哪里运行它,您仍然需要先实例化 Choice。

关于java - 开始 : Applet not initialized error in NetBeans IDE 8. 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24533053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com