Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DefaultListModel problem

0 views
Skip to first unread message

Warpig

unread,
Dec 3, 2001, 4:35:04 AM12/3/01
to
Hi All,

I am having a problem with the DefaultListModel of the JList. I have
declared the as below:

private JList allGroupsList;
private JList activeGroupsList;
private DefaultListModel allGroupsModel;
private DefaultListModel activeGroupsModel;

allGroupsModel = new DefaultListModel();
allGroupsList = new JList(allGroupsModel);
activeGroupsModel = new DefaultListModel();
activeGroupsList = new JList(activeGroupsModel);

I then have a button that when clicked, reads in a file and places the
file's contents into the allGroupsModel object. The code for this is:

try
{
File f = new File("groups.txt");
BufferedReader br = new BufferedReader(new FileReader(f));
StringTokenizer first = new StringTokenizer(br.readLine());
String line = first.nextToken();
int i = 1;
while(!line.equals(""))
{
allGroupsModel.addElement(line);
StringTokenizer st = new StringTokenizer(br.readLine());
line = st.nextToken();
}
br.close();
}
catch(IOException e)
{
System.err.println(e.toString());
}

The "groups.txt" file has approximately 23,000 lines in it and when
loaded, the list contains them all, however, I get the following exception:

Exception occurred during event dispatching:
java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:122)
at java.util.StringTokenizer.<init>(StringTokenizer.java:152)
at GroupDialog.actionPerformed(GroupDialog.java:262)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Window.dispatchEventImpl(Window.java:926)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.Dialog.show(Dialog.java:380)
at PicAgentFrame.actionPerformed(PicAgentFrame.java:260)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Window.dispatchEventImpl(Window.java:926)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)


The second JList (activeGroupsModel) I have contains the selected
components of the allGroupsModel when the add button has been clicked.
Moving the selected componenets from one list to the other works
fine, however, if I try to use the remove button which places selected
components of the activeGroupsModel back into the allGroupsModel, I get
an exception and not all selected components are removed. The code for
this is:

public void addToActiveGroups(String groupName)
{
activeGroupsModel.addElement(groupName);
}

public void addToAllGroups(String groupName)
{
allGroupsModel.addElement(groupName);
}

//this section is within the actionPerformed method
else if(source == addButton)
{
/*
* Add the selected elements from the list of the left
* to the list on the right and then remove the seelcted
* elements from the list on the left
*/
int[] selected = allGroupsList.getSelectedIndices();
System.out.println("ADD : int[] size is " + selected.length);
for(int i = 0; i < selected.length; i++)
{
addToActiveGroups((String)allGroupsModel.elementAt(selected[i]));
allGroupsModel.removeElementAt(selected[i]);
}
}
else if(source == removeButton)
{
/*
* Remove the selected elements from the list on the right
* and replace them into the list on the left. I'll just
* addElement for now, but ideally, I'll insert it into the
* correct position of the left hand sided list as that will
* be sorted
*/
int[] selected = activeGroupsList.getSelectedIndices();
System.out.println("REMOVE: int[] size is " + selected.length);
for(int i = 0; i < selected.length; i++)
{
addToAllGroups((String)activeGroupsModel.elementAt(selected[i]));
activeGroupsModel.removeElementAt(selected[i]);
}
}

The int arrays are reported as the correct size, but not all of the
slected elements are removed. The Exception I get is:

//the next to lines are not part of the exception...
ADD : int[] size is 4
REMOVE: int[] size is 4

Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:417)
at javax.swing.DefaultListModel.elementAt(DefaultListModel.java:248)
at GroupDialog.actionPerformed(GroupDialog.java:311)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Window.dispatchEventImpl(Window.java:926)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.Dialog.show(Dialog.java:380)
at PicAgentFrame.actionPerformed(PicAgentFrame.java:260)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Window.dispatchEventImpl(Window.java:926)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)

I am using JDK1.3.1 on Linux if this is of any help.
I know there is a lot of code and exception stacktrace here, but I was
hoping that someone could shed some light on this - I haven't had much luck!

Thanks in advance!
Regards,
Leigh

Todd Neumiller

unread,
Dec 3, 2001, 10:32:34 AM12/3/01
to
Your BufferedReader is going to return a null and the
end of the file.

Also the BufferedReader is going to strip any eol characters,
so it is hard to tell why you are using a stringtokenizer
when you only get the 1st token of each line?

If each line is intended for one row in your List then you
do not need the stringtokenizer

I would guess (with out seeing the text file contents)

try
{
File f = new File("groups.txt");
BufferedReader br = new BufferedReader(new FileReader(f));

String line = br.readLine();
while(line != null)
{
allGroupsModel.addElement(line);
int i = 1;
line = br.readLine();
}
br.close();
}
catch(IOException e)
{
System.err.println(e.toString());

0 new messages