我有一個JLabels數組,他們似乎工作。如果我做一個System.out.print(days [index]);我收到了包含的實際信息,並且該標籤已存在並可用。將JLabel數組添加到Java中的單個面板中?
當我嘗試在面板的任何索引添加標籤我得到一個空指針異常,我不知道爲什麼?
public class DrawCalendar extends JPanel{
private JLabel month = new JLabel("Month");
private JLabel[] days = {
new JLabel("Sunday"),
new JLabel("Monday"),
new JLabel("Tuesday"),
new JLabel("Wednesday"),
new JLabel("Thursday"),
new JLabel("Friday"),
new JLabel("Saturday")
};
private JPanel dayBoxes;
private JPanel topLabels;
public DrawCalendar(int month){
topLabels.add(days[1]); //the NullPointerException caused here
add(topLabels);
}
}
*捂臉*感謝 – leigero