2013-04-14 62 views
0

在我的GUI中,我有3個單選按鈕,它們是同一個ButtonGroup的一部分。我試圖得到它,以便當任何一個按鈕被點擊時,一個JList將出現。在我的動作監聽器中,我編寫了JLists,但是當我嘗試運行它時,出現NullPointerException。我嘗試將JList放置在動作偵聽器之外,並且它工作正常,所以我很確定我的動作偵聽器出現了問題。我的按鈕出現在GUI中。單擊JRadioButton時更改JLists

這是主要的GUI類中的內部類:

rbGenCancers = new JRadioButton("genCan"); 
    rbRelCancers = new JRadioButton("relCan"); 
    rbCancers = new JRadioButton("nonC"); 
    ButtonGroup bg = new ButtonGroup(); 
    bg.add(rbGenCancers); 
    bg.add(rbRelCancers); 
    bg.add(rbCancers); 

    JPanel rbButtons = new JPanel(new FlowLayout(3, 2, 2)); 
    rbButtons.add(rbGenCancers); 
    rbButtons.add(rbRelCancers); 
    rbButtons.add(rbCancers); 
    //added to the JFrame correctly through GridBagLayout and GridBagConstraints 

public class RadioButtonListener implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     switch(e.getActionCommand()) { 
      case "genCan": 
       listModel = new DefaultListModel(); 
       for(int i = 0; i < 8; i++) { 
        cancerNames = _controller.selectGeneralCancer(i); 
        listModel.addElement(cancerNames); 
       } 
       cancerList = new JList(listModel); 
       cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
       cancerList.setSelectedIndex(-1); 
       cancerList.setVisibleRowCount(3); 
       cancerListScroller = new JScrollPane(cancerList); 
       break; 
      case "relCan": 
       listModel = new DefaultListModel(); 
       for(int i = 0; i < 22; i++) { 
        split = _controller.readCancer(i); 
        cancerNames = split[0]; 
        listModel.addElement(cancerNames); 
       } 
       cancerList = new JList(listModel); 
       cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
       cancerList.setSelectedIndex(-1); 
       cancerList.setVisibleRowCount(3); 
       cancerListScroller = new JScrollPane(cancerList); 
       break; 
      case "nonC": 
       listModel = new DefaultListModel(); 
       for(int i = 22; i < 36; i++) { 
        split = _controller.readCancer(i); 
        cancerNames = split[0]; 
        listModel.addElement(cancerNames); 
       } 
       cancerList = new JList(listModel); 
       cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
       cancerList.setSelectedIndex(-1); 
       cancerList.setVisibleRowCount(3); 
       cancerListScroller = new JScrollPane(cancerList); 
       break; 
     } 
    } 

} 

下面是拋出的異常:

Exception in thread "main" java.lang.NullPointerException 
at java.awt.Container.addImpl(Container.java:1086) 
at java.awt.Container.add(Container.java:998) 
at javax.swing.JFrame.addImpl(JFrame.java:562) 
at java.awt.Container.add(Container.java:966) 
at Cancer.gui.CancerGUI.<init>(CancerGUI.java:306) 
at Cancer.run.CancerApp.main(CancerApp.java:19) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 2 seconds) 

這裏是執行所有代碼的類。 printstack中的第19行對應於CancerGUI _gui = new CancerGUI(); CancerGUI中的第306行對應於我通過GridBagConstraints將cancerListScroller添加到JFrame的位置。

public class CancerApp { 
public static void main(String[] args) { 
    CancerGUI _gui = new CancerGUI(); 
    Disease _disease = new Disease(); 
    CancerController _controller = new CancerController(_disease, _gui); 
    _gui.setController(_controller); 

    CancerApp _app = new CancerApp(); 
    _app.run(_gui); 
} 

public void run(CancerGUI _gui) { 
    _gui.setVisible(true); 
} 
} 
+0

錯誤發生在'CancerGUI'構造函數中。你可以發佈,請。 – christopher

+0

爲了儘快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

0

我有同樣的想法。單選按鈕可以工作,但我的動作偵聽器似乎沒有用setModel(listmodel)設置一個新列表。我沒有例外。當我得到一個異常時,這是因爲類型不匹配。這些可能會令人不快。我會仔細檢查一下,以確保消息正在通過,但它應該。

else if(ae.getActionCommand().equals("chooseTitle")) 
{ 
    jlst.setModel(lm); 
} 

它可能工作正常,但我必須做些什麼才能使其顯示?