Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException in TreeModel after removeNodeFromParent()  RSS feed

 
Anil Philip
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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


 
Anand Loni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!