【发布时间】:2013-02-06 15:44:54
【问题描述】:
我有一个包含以下类的摇摆应用程序:
ControllerGUI:加载我的主窗体
日期:用于将输入的数字转换为日期格式
Employee:扩展person类并设置变量,如薪水、职位
主窗体:swing gui
人员:为其他详细信息设置变量,例如姓名、性别、出生日期
Store:存放数组Employee[]列表;
我有一个 displayInformation JPanel,我想在商店的 JTextFields 中显示姓名、薪水、职位等,然后允许用户使用下一个和上一个按钮浏览条目。但是,当我试图让它首先工作时,我遇到了 NullPointerException。
在我的 MainForm 中我添加了一个新的 Store
Store testStore = new Store(100);
我希望将数组中的元素输出到不同的 JTextField,例如:
showName.setText(testStore.list[listIndex].name);
(其中 listIndex 是我发起的 int) 但是从那以后我得到了一个 NullPointerException,如果我去掉 .name 或 .salary 任何我想得到的东西,错误就会消失,但显然代码中没有任何意义。
这里的任何帮助将不胜感激!
public class Store implements Serializable {
private static int MAXSIZE; // holds the size of the array
private static int count; // keeps count of number of persons stored in
// array
Employee[] list; // array for storing person objects
public Store(int size) {
list = new Employee[size];
MAXSIZE = size;
count = 0;
}
【问题讨论】:
-
我们需要看看列表是如何初始化的,您是否希望数组的那个元素有一个有效的员工?
-
将代码添加到我的帖子底部。我要做的就是在添加员工之后,让用户能够在单独的 JPanel 中查看详细信息,因此将数组中的每个详细信息(姓名、出生日期等)加载到文本字段中。所以当然当程序启动时数组是空的
标签: java arrays swing nullpointerexception settext