Java Help Needed
Currently I have two JLists, one for selecting items and the other contains the chosen items. Although I have followed the standard in acquiring the selected index via the JList and can see the correct index, I still get a null pointer exception when I remove the item. I am able to add an item, add all items and remove all items using the DefaultListModel, but for some reason, removing a particular element at a specified index produces a null pointer exception. Any help will be greatly appreciated. I am using v 1.4.0
int chosenIndex = chosenOdorantsJList.getSelectedIndex();
listModel.removeElementAt(chosenIndex);
I also have another question, I currently have nine main methods with graphical user interfaces, and would like to create a main GUI, similar to the one that Java Web Start 1.0.1 has. It has a small main GUI which can call other GUI's which are much larger. I have tried looking on the net and was not able to find much on this topic.
[995 byte] By [
SpartA] at [2007-9-27 1:43:04]

the gist of it-
private class ChosenOdorantsListListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent lse)
{
if( !lse.getValueIsAdjusting() )
{
chosenOdorant = chosenOdorantsJList.getSelectedValue().toString();
chosenIndex = chosenOdorantsJList.getSelectedIndex();
//chosenIndex = listModel.indexOf(chosenOdorant);//trying to see the difference
//System.out.println(selection);
}
}
}
//this function will remove the selection
private void remove(String chosenOdorant)
{
if( chosenIndex >= 0 )
{
System.out.println("chosen index = " + chosenIndex );
System.out.println("chosen odorant = " + listModel.elementAt(chosenIndex) );
listModel.removeElementAt(chosenIndex);
}
}
I too think that the problem should be with your chosenIndex, not with the listmodel's removeElement method. Since you are storing the value of the selected index into a variable, and later refer to it in delete action method, the index might have changed in between. I don't know your real situation - Try to put debug statements wherver the chosenIndex is used or updated and print the value of the chosenIndex, and check it with the selected element. Hope that this would help you to solve your problem.
Please don't forget the dukes...
private class ChosenOdorantsListListener implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent lse)
{
if( !lse.getValueIsAdjusting() )
{
if( chosenOdorantsJList.getSelectedValue() != null )
{
chosenOdorant = chosenOdorantsJList.getSelectedValue().toString();
chosenIndex = listModel.indexOf(chosenOdorant);
}
}
}
}