私はリストを持っています:
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全体に慣れていないだけですが、これはすべきではありません私は何も新しくしなかったので、これがどのオブジェクトであるかについて特定しただけなので、私にとっては問題になります。
誰かが私が自分自身を解くのを手伝ってくれませんか?