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

Help! exception driving me nuts!

0 views
Skip to first unread message

Richard Reynolds

unread,
Jan 12, 2002, 9:52:30 AM1/12/02
to
Anyone any idea what's going on with the following?
I've a user defined class (QUBSystem) that contains a DefaultMutableTreeNode
(the root), a TreeModel based on this and a JTree based on the model.
I set up the system so it has a root and 1 child and this is displayed in
the front end.
I then save the system to a file (serialised).
If I then try to remove the child I get an ArrayOutOfBounds exception (shown
below). If I don't do the save first I can remove the child with no
exceptions.
I've now put the save operation into the event dispatch thread to check if
it was something thread related but it hasn't helped.
Surely saving the system to the file doesn't change the treeModel so why do
I only get the exception after a save?
Please don't be put off by the code length, it's nearly all exception
handling, there's only about 3 relevant lines in each of the 3 methods.
Thanks for any ideas,
I'm using jdk1.4beta-3 Here's the relevant code, it's very straightforward :

The save method in the QUBSystem class - called form an actionPerformed
method in the front end class
/**
* Saves this system to a file in serialised form, any exceptions while
* saving this system are notified to the user except for exceptions
when
* closing streams. ".qub" will be appended to the filename if
* it is not already the filename extension.
*
* @param fFile the file to save the system to
*/
public void saveSystem(File fFile)
{
ObjectOutputStream objStream = null; // to write to a file

if(! FileUtils.getExtension(fFile).equals("qub"))
{ // add .qub to the filename if it's not already there
fFile = new File(fFile.getAbsolutePath()+".qub");
}
try
{
objStream = new ObjectOutputStream(new FileOutputStream(fFile));
objStream.writeObject(this); // write to the file
}
catch(Exception ex)
{
try
{
throw new GUIException
("There was a problem saving this system" +
IQUBTraceConstants.NEW_LINE +
"the error message encountered was : " +
IQUBTraceConstants.NEW_LINE + ex.getMessage());
}
catch(GUIException ex1){ /* handled in the ex. class */ }
}
finally
{
try
{
if(null != objStream)
{
objStream.close();
}
}
catch(IOException ignored){} // user won't want to know
}
}

The remove nodes method in the QUBSystem class (the nodes to be removed are
always files)
/**
* Removes files from the JTree associated with this system.
*
* @param vFilesToRemove the paths to the files to be removed
*/
public void removeFiles(TreePath[] vPaths)
{
for(int i=0; i<vPaths.length; i++)
{
OpeningFrame.setStatus("removing "+vPaths[i].toString()+" ...
");
MutableTreeNode oNode
=(MutableTreeNode)(vPaths[i].getLastPathComponent());
oTreeModel_i.removeNodeFromParent(oNode);
}
}

the removeFiles actionPerformed method in the front end
/**
* Handles a remove files action, removes files from a system.
*
* @param e the action event
*/
public void actionPerformed(ActionEvent e)
{
try
{
// get the currently selected components
TreePath[] vSelectedPaths = oSystem_i.getSelections();

// check that all the selected components are parsedFiles
oSystem_i.getNotParsedFiles(vSelectedPaths);

setStatus("removing files ... ");
// got to here so everyting's OK, remove the selected files
oSystem_i.removeFiles(vSelectedPaths);

// check if there are no ParsedFiles left in the system
if(0 >= oSystem_i.getChildCount())
{ // disable appropriate actions as there are no files
left
enableFileActions(false);
}
}
catch(GUIException ex) {/* Handled in the Exception class */}
finally
{
setStatus("idle");
}
}

The exception I get -
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:422)
at
javax.swing.tree.DefaultMutableTreeNode.getChildAt(DefaultMutableTreeNode.ja
va:233)
at
javax.swing.tree.VariableHeightLayoutCache.treeNodesRemoved(VariableHeightLa
youtCache.java:546)
at
javax.swing.plaf.basic.BasicTreeUI$TreeModelHandler.treeNodesRemoved(BasicTr
eeUI.java:2456)
at
javax.swing.tree.DefaultTreeModel.fireTreeNodesRemoved(DefaultTreeModel.java
:533)
at
javax.swing.tree.DefaultTreeModel.nodesWereRemoved(DefaultTreeModel.java:311
)
at
javax.swing.tree.DefaultTreeModel.removeNodeFromParent(DefaultTreeModel.java
:247)
at uk.ac.qub.trace.utils.QUBSystem.removeFiles(QUBSystem.java:403)
at
uk.ac.qub.trace.gui.OpeningFrame$RemoveFilesAction.actionPerformed(OpeningFr
ame.java:784)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1770)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButto
n.java:1823)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:4
22)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:260)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener
.java:261)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:230)
at java.awt.Component.processMouseEvent(Component.java:5020)
at java.awt.Component.processEvent(Component.java:4819)
at java.awt.Container.processEvent(Container.java:1383)
at java.awt.Component.dispatchEventImpl(Component.java:3527)
at java.awt.Container.dispatchEventImpl(Container.java:1440)
at java.awt.Component.dispatchEvent(Component.java:3368)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3219)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2930)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2866)
at java.awt.Container.dispatchEventImpl(Container.java:1426)
at java.awt.Window.dispatchEventImpl(Window.java:1568)
at java.awt.Component.dispatchEvent(Component.java:3368)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:448)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.ja
va:193)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java
:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:141)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:133)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:101)


Richard Reynolds

unread,
Jan 12, 2002, 5:38:22 PM1/12/02
to
It's ok, sorted, the JTree didn't seem to serialize properly, at least
that's what I think was wrong cause when I wrote the root to the file
instead and recreated the tree from it everything worked ok!

"Richard Reynolds" <richier...@ntlworld.com> wrote in message
news:K3Y%7.1998$Hg7.2...@news11-gui.server.ntli.net...

0 new messages