Results 1 to 4 of 4
- 05-05-2011, 02:28 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
NullPointerException with treeNode error
hello everyone. i have a method that I run. the first time i run it, it runs fine. the second time its run, i get this error:
java.lang.NullPointerException
I'm just adding a tree node to a jTree. below is the code snippet that throws the error:
any ideas? thanksJava Code:DefaultMutableTreeNode treeNode1 = new DefaultMutableTreeNode("Incident Report"); try{ jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1)); }catch(Exception e){ System.out.print(e); }
jason
- 05-05-2011, 05:04 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
A null pointer exception occurs when you treat a variable that has no value as if it had a value. For instance using the . dereferencing operator, or using [] in the case of an array. Basically you have to figure out what variable is null.
For one thing you should change your catch block so that it gives you more information. Also you could check the value of jTree1 which is a likely candidate.
Java Code:DefaultMutableTreeNode treeNode1 = new DefaultMutableTreeNode("Incident Report"); try{ [color=blue]System.out.println("about to set model, jTree1=" + jTree1)[/color] jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1)); }catch(Exception e){ [color=blue]e.printStackTrace();[/color] //System.out.print(e); }
Once you have the variable which is null you can figure out where you thought you had assigned a nonnull value and figure out why that did not happen.
(These NB (?) inspired names are horrible. Putting digits at the end is no way to keep one tree distinct from another. Fully specifying classes is code clutter. It would be better to import javax.swing.tree.DefaultTreeModel and use the simple name: DefaultTreeModel.)
- 05-05-2011, 05:55 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 64
- Rep Power
- 0
thanks
Thanks for the help!
Using the printStackTrace helped me to locate the error. As it turned out, further down the code I was attaching a treeSelectionListener that was null.
Thanks again!
- 05-05-2011, 09:27 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
cast string to treeNode
By jasonwucinski in forum Advanced JavaReplies: 1Last Post: 04-26-2011, 11:51 PM -
equals() failing in userObject of TreeNode
By PrinceSendai in forum AWT / SwingReplies: 1Last Post: 12-25-2010, 08:57 PM -
Need Help with NullPointerException Error
By waterisgood5 in forum New To JavaReplies: 2Last Post: 11-10-2010, 07:27 PM -
NullPointerException error
By Aggror in forum New To JavaReplies: 2Last Post: 09-29-2010, 02:31 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM


LinkBack URL
About LinkBacks

Bookmarks