Reputation: 1856
I have a jlist that holds a bunch of username strings. I dont have a lot of experience with this, so here is the code making 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);
There is a button next to the jlist, that when i click it, it calls
System.out.println(jlistobject.getSelectedValue().toString();
then i'm getting a nullpointerexception, that it is unable to print that value. i'm not sure what the null pointer is to, but here is the result:
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)
What am I doing wrong? i'm clicking on the item in the list, and clicking this button to have it print, and it has a null pointer effect. should instead i have the value of the list item load to a string object when i click the list item?
please help!
Upvotes: 0
Views: 3391
Reputation: 1502016
From the docs for JList.getSelectedValue()
(emphasis mine):
Returns the value for the smallest selected cell index; the selected value when only a single item is selected in the list. When multiple items are selected, it is simply the value for the smallest selected index. Returns null if there is no selection.
So if getSelectedValue()
is returning null
, you're calling toString()
on a null reference, hence the exception.
The toString()
call is unnecessary anyway - just use:
System.out.println(jlistobject.getSelectedValue());
... that will handle null
references fine, and call toString()
where necessary.
EDIT: If you really think the list has a selected value, changing to the above code will still help to make diagnosis easier. You can also use:
System.out.println(jlistobject.getSelectedIndex());
... if that prints out -1, it confirms that there's no selection. (Maybe you've actually got two separate JList
objects without realizing it, and the one you're selecting from isn't the same as the one you're checking, for example...)
Upvotes: 2