獲得元素我有下面的類Swing應用程序時:
ControllerGUI:加載我的MainForm
日期:用於數字從輸入轉換成日期格式
員工:延長人類和設置變量如工資,職位
MainForm:swing gui
Person:爲其他詳細信息設置變量,如姓名,性別,dob
Store:保存數組Employee [] list;NullPointerException異常試圖從商店
我有一個displayInformation JPanel,我想從商店中顯示JTextFields中的名稱,工資,職位等,然後允許用戶使用下一個和上一個按鈕瀏覽條目。但是,當我試圖讓它在第一個地方工作時,我遇到了NullPointerException。
在我的MainForm我添加了一個新的商店
Store testStore = new Store(100);
我想要的數組中的元素進行outputed不同JtextField中,如:
showName.setText(testStore.list[listIndex].name);
(其中列表索引是我發起的一個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;
}
我們需要看清楚這個列表是如何初始化的,你是否期望在該數組元素處有一個有效的僱員? – cowls
將代碼添加到我的帖子底部。我想要做的就是在添加一名員工之後,爲了讓用戶能夠在單獨的JPanel中查看詳細信息,請將數組中的每個細節(名稱,dob等)加載到文本字段中。 所以當然當程序啓動的時候數組是空的 – Gethin