• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException in TreeModel after removeNodeFromParent()

 
Ranch Foreman
Posts: 560
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have been stuck for more than a few days on this problem. Tried to get help elsewhere, without luck - wondered if anyone here would have insight into this. I promise I shall update the forum if I get a solution.


Please Note that node is NOT null otherwise NP would have been thrown in the node.getParent(). I am mystified to get this NP exception within the Swing lib. Any insights?
thanks,
Anil



----------

I wonder if it may be because when I create the JPanel which contains the tree, I dont have the root node created. So I instantiate the tree model with null;





later on, when the root node is available, I use




This works fine during display of the tree, but when I try to remove the node, for some reason, it may not have updated the root value (?). Just guessing... But I wonder how to fix it. Can I add the Scroll Pane with null first and then with the actual JTree later on? (there is no method in JScrollPane to reset the Component)



-----------
When I try to remove the node differently - using the methods in DefaultMutableTreeNode,





I still get this exception in the treeModel.nodesWereRemoved() call.
Any insight?
thanks,
Anil


 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

before removing node from parent u have to find if it has any sibling or not if no sibling then assign sibling as parent.

***********
public MutableTreeNode getSibling(DefaultMutableTreeNode current)
{
//get previous sibling if any
MutableTreeNode previous=current.getPreviousSibling();
if(previous==null)
{
previous=current.getNextSibling();
}
return previous;
}

************

in delete function code do the following:

**********

MutableTreeNode selected=(MutableTreeNode)tree.getLastSelectedPathComponent();
MutableTreeNode parent = (MutableTreeNode) selected.getParent();
MutableTreeNode sibling= getSibling(selected);
//if no sibling then assign parent to it
if(sibling==null)
{
sibling = parent;
}
treeModel.removeNodeFromParent(selected);
treeModel.reload();
*********************

Hope this will work

Regards,
 
Anil Philip
Ranch Foreman
Posts: 560
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anand Loni:
[QB]Hi,

before removing node from parent u have to find if it has any sibling or not if no sibling then assign sibling as parent.

if(sibling==null)
{
sibling = parent;
}
treeModel.removeNodeFromParent(selected);
treeModel.reload();
[QB]



Sorry, but this doesnt make sense. What does that code do?
-
Anil
 
Anil Philip
Ranch Foreman
Posts: 560
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have reproduced the problem in the toy program below. Click on any button, you will see the exception at bottom.




 
Anil Philip
Ranch Foreman
Posts: 560
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is fixed!




I had to simply setSelectionPath to null. Anand, God bless you, I dont know if that's what you meant, but that fixed it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic