这是一个应用程序,其中包括个性列表。一旦启动,个性数据显示在左侧,有两个列表:一个包含所有类别的个性,另一个包含此列表中的所有名称。用户在每个JList中选择一行,他点击按钮“搜索”...然后代码调用findByCriteria ....但是在这里,我有一个NullPointerException ......经过一些研究,我意识到这是因为我的列表很差或没有初始化...我看在我的代码,但我不明白它是怎么回事,因为在调用之前必须初始化的方法的调用是我的Button上的监听器...最奇怪的是,当我运行应用程序时,列表被填充。我使用MVC模式和方法更新...
以下是我视图中的代码:
modelListCat = new DefaultListModel();
listCategories = new JList();
listCategories.setName("category");
scrollPaneCat.setViewportView(listCategories);
listCategories.setPreferredSize(new Dimension(0, 90));
listCategories.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listCategories.setLayoutOrientation(JList.HORIZONTAL_WRAP);
listCategories.setVisibleRowCount(-1);
PersonalitiesController.getInstance().setJListForCategories(personalitiesListCat, listCategories, modelListCat);
再远一点:
modelListNames = new DefaultListModel();
listNames = new JList();
listNames.setName("name");
scrollPaneN.setViewportView(listNames);
listNames.setPreferredSize(new Dimension(0, 110));
listNames.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listNames.setSelectedIndex(0);
listNames.setLayoutOrientation(JList.HORIZONTAL_WRAP);
listNames.setVisibleRowCount(-1);
PersonalitiesController.getInstance().setJListForNames(personalitiesListNames, listNames, modelListNames);
btnSearch = new JButton("Rechercher");
btnSearch.setActionCommand("search");
btnSearch.setName("search");
GridBagConstraints gbc_btnSearch = new GridBagConstraints();
gbc_btnSearch.insets = new Insets(0, 0, 5, 5);
gbc_btnSearch.gridx = 0;
gbc_btnSearch.gridy = 4;
westPanel.add(btnSearch, gbc_btnSearch);
this.btnSearch.addActionListener(PersonalitiesController.getInstance());
在PersonalitiesController中初始化JList类的方法代码(这与JList名称相同):
public void setJListForCategories(List<TPersonalities> personalitiesListCat, JList listCategories, DefaultListModel modelListCat){
modelListCat = new DefaultListModel();
modelListCat.addElement("Toute");
for(int i = 0; i < personalitiesListCat.size(); i++){
modelListCat.addElement(personalitiesListCat.get(i));
}
listCategories.setModel(modelListCat);
setListNames(listCategories);
}
以下是PersonalitiesController中的ActionPerformed代码:
public void actionPerformed(ActionEvent ev) {
// TODO Auto-generated method stub
//JButton button = (JButton)ev.getSource();
if("search".equals(ev.getActionCommand())){
DefaultListModel modelListCat = (DefaultListModel)listCategories.getModel();
DefaultListModel modelListNames = (DefaultListModel)listNames.getModel();
int selected = this.listCategories.getSelectedIndex( );
if(selected != -1){
for(int i = 0; i != listCategories.getSelectedIndex(); i++){
this.category = (String)modelListCat.getElementAt(selected);
}
}
int selected2 = this.listNames.getSelectedIndex( );
if(selected2 != -1){
for(int j =0; j != listNames.getSelectedIndex(); j++){
this.name = (String)modelListNames.getElementAt(selected2);
}
}
if(category.equalsIgnoreCase("Toute") && category == ""){
if(!name.equalsIgnoreCase("Tout") && name != ""){
model.findPersonalitiesByCriteria(name, null, null);
}
}else{
if(!name.equalsIgnoreCase("Tout") && name != ""){
model.findPersonalitiesByCriteria(name, category, null);
}else{
model.findPersonalitiesByCriteria(null, category, null);
}
}
}
}
以下是错误消息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.cultureAtBrussels.controller.PersonalitiesController.actionPerformed(PersonalitiesController.java:54)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
ligne 54是:
DefaultListModel modelListCat = (DefaultListModel)listCategories.getModel();