2012-12-14 5 views
1

JLabel 배열이 있는데, 작동하는 것 같습니다. System.out.print (days [index]); 나는 실제 정보가 포함되어 있고 라벨이 있고 작동합니다.Java의 단일 패널에 JLabel 배열 추가?

패널에 어떤 인덱스에서나 레이블을 추가하려고하면 널 포인터 예외가 발생하며 그 이유를 모르겠습니다.

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

    } 
} 

답변

1

topLabels가 인스턴스화되지 않았습니다. JPanel 유형이지만

topLabels = new JPanel(); 

까지는 null이 아닙니다.

+0

* 마른 세수 * 감사 – leigero

1

private JPanel topLabels;은 초기화 되었습니까? 당신은 아마 비슷한 원하는 : 당신의 DrawCalendar의 생성자

topLabels = new JPanel(); 

을하거나 선언 라인에 암시를 수행

private JPanel topLabels = new JPanel();