0

我对编程有点陌生,如果有一些事情可以做得更好,我很抱歉我的组合框已成功填充我的字符串数组,并且自动完成工作正常。我只是无法在组合框中获取文本。

返回 java.lang.NullPointerException

private ArrayList<String> arrRekening;
private ArrayList<String> arrEienaar;
private String[] sarrRekening;
private String[] sarrEienaar;

    public NewConnectionPoint() {

    arrAccount = new ArrayList<String>();
    arrOwner = new ArrayList<String>();

    FillCombo(arrAccount , "Owners", "OwnerName");
    FillCombo(arrOwner , "Accounts", "AccountName");
    sarrOwner= arrOwner.toArray(new String[arrOwner .size()]);
    sarrAccount= arrAccount.toArray(new String[arrAccount.size()]);

    JComboBox<String> comboAccount = new JComboBox<String>();
    AutoCompleteSupport<String> supAccount = AutoCompleteSupport.install(comboRekening, GlazedLists.eventList(Arrays.asList(sarrAccount)));
    supAccount.setStrict(true);

    JComboBox<String> comboOwner = new JComboBox<String>();
    AutoCompleteSupport<String> supOwner = AutoCompleteSupport.install(comboOwner,GlazedLists.eventList(Arrays.asList(sarrOwner)));
    supOwner.setStrict(true);

    JButton btnShow = new JButton("ShowSelectedr");
    btnShow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) { 
        //Error occurs at this line 
            JOptionPane.showMessageDialog(null, comboOwner.getSelectedItem().toString());

    });}

}

//使用sql将数据从数据库加载到arraylists

private void FillCombo(ArrayList<String> ComboElements, String sTable, String sColumn){
try{
    Data.changeQuery(sTable);// database connection fine returns and fills combobox 

    while(MyData.rs.next()){
        String sReturn= MyData.rs.getString(sColumn);
        ComboElements.add(sReturn);
    }

}catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
}

}

4

1 回答 1

1

您在这里遇到的根本困难是您试图利用 GlazedLists 包而没有正确地接受它的核心实用程序:EventLists。

如果您使用 EventLists 而不是 ArrayLists,您可以轻松地避开您的困难。

如果你真的想要,你可以让你的方法返回一个ArrayList FillCombo(也许更好的名字为getElements()工作正常。

将列表连接到具有自动完成支持的组合框的修改后的代码如下所示:

...
FillCombo(arrOwner , "Owners", "OwnerName");
EventList<String> ownerEventList = GlazedLists.eventList(arrOwner);
EventComboBoxModel<String> ownerModel = new EventComboBoxModel<String>(ownerEventList);
JComboBox comboOwner = new JComboBox(ownerModel);
AutoCompleteSupport<String> supOwner = AutoCompleteSupport.install(comboOwner,ownerEventList);
supOwner.setStrict(true);
...
于 2014-12-23T10:14:08.293 回答