0

这是一个应用程序,除其他外,还有一个个性列表。启动后,左侧显示一个人物的数据,有两个列表:一个包含所有人物类别,另一个包含此列表中的所有名称。用户在每个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)

54 法分是:

DefaultListModel modelListCat = (DefaultListModel)listCategories.getModel(); 
4

0 回答 0