Results 1 to 4 of 4
  1. #1
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default 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:

    Java Code:
    DefaultMutableTreeNode treeNode1 = new DefaultMutableTreeNode("Incident Report");       
             
             try{
                jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));    
             }catch(Exception e){
                
               System.out.print(e);              
             }
    any ideas? thanks
    jason

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.)

  3. #3
    jasonwucinski is offline Member
    Join Date
    Feb 2011
    Posts
    64
    Rep Power
    0

    Default 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!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. cast string to treeNode
    By jasonwucinski in forum Advanced Java
    Replies: 1
    Last Post: 04-27-2011, 12:51 AM
  2. equals() failing in userObject of TreeNode
    By PrinceSendai in forum AWT / Swing
    Replies: 1
    Last Post: 12-25-2010, 09:57 PM
  3. Need Help with NullPointerException Error
    By waterisgood5 in forum New To Java
    Replies: 2
    Last Post: 11-10-2010, 08:27 PM
  4. NullPointerException error
    By Aggror in forum New To Java
    Replies: 2
    Last Post: 09-29-2010, 03:31 PM
  5. Replies: 1
    Last Post: 12-05-2009, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •