0

我有一个清单:

JList characterList = new JList(characterListModel);
characterListModel = new DefaultListModel();        
String myCharacters[]={"Dean Winchester","Sam Winchester",
            "Bobby Singer","Castiel"};
for (String myCharacter : myCharacters) {
        ((DefaultListModel) characterList.getModel()).addElement(myCharacter);  
}

而且我在单独的类中编写了一个方法来删除选定索引处的字符:

public void removeCharacter() {
    DefaultListModel characterListModel = ((PlayerContentPane) IViewManager.Util.getInstance()
        .getMyContainerPane().getMyPlayerManagerPane().getContentPane())
        .getCharacterListModel();
    JList characterList = ((PlayerContentPane) IViewManager.Util.getInstance()
        .getMyContainerPane().getMyPlayerManagerPane().getContentPane())
        .getCharacterList();
    int idx = characterList.getSelectedIndex(); //<---line 62
    int size = characterListModel.getSize();
    characterListModel.remove(idx);
    if (size == 0) { 
        //do nothing
    } else { 
        if (idx == characterListModel.getSize()) {
            idx--;
        }

        characterList.setSelectedIndex(idx);
        characterList.ensureIndexIsVisible(idx);
    }

}

然而,当我用我的按钮运行它时,我得到了这个堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.detica.LarpDB.Controller.Controller.removeCharacter(Controller.java:62)
at com.detica.LarpDB.view.PlayerContentPane$3.actionPerformed(PlayerContentPane.java:94)
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$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$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)

我在谷歌上看到的很多问题都源于以下一行:DefaultListModel characterListModel = .... 他们遇到的问题是他们创建了一个new对象的实例,现在我只是对整个 Java 事物的新手,但这不应该对我来说是个问题,因为我没有做出任何新的东西,我只是具体说明了这是哪个对象。

请问有人可以帮我解开自己吗?

4

2 回答 2

3

这两行可能会产生很多问题:

DefaultListModel characterListModel = ((PlayerContentPane) IViewManager.Util.getInstance()
    .getMyContainerPane().getMyPlayerManagerPane().getContentPane())
    .getCharacterListModel();
JList characterList = ((PlayerContentPane) IViewManager.Util.getInstance()
    .getMyContainerPane().getMyPlayerManagerPane().getContentPane())
    .getCharacterList();

如果任何链接方法返回null(无论出于何种原因),该行将抛出 NullPointerException。

我建议你将它们分成几行并检查每个调用的值,看看你从哪里得到的null

编辑

刚刚意识到第 62 行是int idx = characterList.getSelectedIndex();. 这意味着它characterList是空的。

于 2012-07-23T15:02:46.670 回答
2

如果它在您指示的行失败是第 62 行,那么该行上引用的唯一对象,因此唯一可能为 null 的对象是 characterList。这意味着您的 getCharacterList 函数返回 null。(如果错误在 getSelectedIndex 函数内,那么您的堆栈跟踪中将有另一行。)

由于您没有发布 getCharacterList 函数,所以我不能多说。但是你应该看看那个函数,看看在什么情况下它会返回 null。

于 2012-07-23T15:08:55.447 回答