我有一个包含一堆用户名字符串的jlist。我没有很多这方面的经验,所以这里是代码制作jlist:
for (int i = 0; i < usersArray.length; i++) {//usersArray is String[] loaded from a list of username strings
defaultListModelObject.addElement(usersArray[i]);
}
jlistobject = new JList(defaultListModelObject);
jlist旁边有一个按钮,当我点击它时,它会调用
System.out.println(jlistobject.getSelectedValue().toString();
然后我得到一个nullpointerexception,它无法打印该值。我不确定空指针是什么,但结果如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at avtech.software.minecraft.server.components.UsersListPanel.getSelectedPlayer(UsersListPanel.java:56)
at avtech.software.minecraft.server.components.PlayerCommandPanel.player(PlayerCommandPanel.java:125)
at avtech.software.minecraft.server.components.PlayerCommandPanel.access$0(PlayerCommandPanel.java:124)
at avtech.software.minecraft.server.components.PlayerCommandPanel$3.actionPerformed(PlayerCommandPanel.java:44)
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$200(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)
我做错了什么?我点击列表中的项目,然后单击此按钮使其打印,并且它具有空指针效果。我应该在单击列表项时将列表项的值加载到字符串对象吗?
请帮忙!
答案 0 :(得分:2)
来自JList.getSelectedValue()
的文件(强调我的):
返回最小的选定单元格索引的值;在列表中仅选择单个项目时所选的值。选择多个项目时,它只是最小选定索引的值。 如果没有选择,则返回null。
因此,如果getSelectedValue()
返回null
,则您在空引用上调用toString()
,因此异常。
无论如何都不需要toString()
来电 - 只需使用:
System.out.println(jlistobject.getSelectedValue());
...将处理null
个引用,并在必要时调用toString()
。
编辑:如果您确实认为该列表具有选定值,则更改为上述代码仍有助于简化诊断。您也可以使用:
System.out.println(jlistobject.getSelectedIndex());
...如果打印出-1,则确认没有选择。 (也许你实际上有两个单独的JList
对象没有意识到它,而你选择的对象与你正在检查的对象不同,例如......)