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]
# 1
Keep in mind that I do check to see if the index is greater than or equal to zero...if( chosenIndex >= 0 )
SpartA at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...
# 2

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);

}

}

SpartA at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...
# 3
It could be that the method you are calling to remove the item is deprecated or not functioning.
tjacobs01 at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...
# 4
Java is not telling me that it's deprecated, but listModel.removeElementAt(int index), sort of works but it returns a null pointer exception
SpartA at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...
# 5
I have also tried the other removes, those also give me a null pointer exception.
SpartA at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...
# 6

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...

siraj_java at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...
# 7
make sure to remove all selection listeners from the list before removing items.then add them again after.
killroyboy at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...
# 8

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);

}

}

}

}

SpartA at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...
# 9
Check for null
spart_arguello at 2007-7-4 20:02:49 > top of Java-index,Archived Forums,Swing...