aspose file tools*
The moose likes Java in General and the fly likes null pointer exception while closing  a node of jtree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "null pointer exception while closing  a node of jtree" Watch "null pointer exception while closing  a node of jtree" New topic
Author

null pointer exception while closing a node of jtree

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692



at the above line my application is throwing an exception , i had make ftp app , so whenever i close any of the tree's node , remember that icon that we use to close the jtree's node the one which has leaf nodes inside it , but i m not getting why is it throwing an null pointer exception while at the time of closing a node ??
any answers ?

but this exception doesnt affect my work or app , my app still works fine , but thing is making me curious to know what is happening , even though there is an exception my app is working fine
but still any how i need to find out the root cause of this exception ,
one more thing this statement is in the treeselectionlistener() method and gives me the last value selected by the user
i have also check whether closing the node gives me null or what , but no it is not giving any null value , but still exception is thrown , why ???




The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

Well, something on line 808 of file JTreeWithFTP.java is null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Rob Spoor wrote:Well, something on line 808 of file JTreeWithFTP.java is null.


ya , this is line no 808
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
So, node is null at this point. Or, there is something in the getUserObject() method which is null and causing an Exception. Since 808 appears to be the top line in your stack trace, the first explanation is probably correct and the second probably incorrect.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Campbell Ritchie wrote:So, node is null at this point. Or, there is something in the getUserObject() method which is null and causing an Exception. Since 808 appears to be the top line in your stack trace, the first explanation is probably correct and the second probably incorrect.

ya , but what can i do to stop this exception to be thrown ?
it is a jtree , so what could be null while closing the node , i m not getting it ?
i know null porinter exception is thrown , when i i dont create any object but use the reference all over my code
but here i m not getting it
can you help me campbell finding and fixing this problem ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Start with line 807½See what that prints.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Campbell Ritchie wrote:Start with line 807½See what that prints.

ya , i got why it is throwing null pointer exception because when you browse inside a node , under it , there is leaf nodes , so when you select any of the leaf node
this line fetch the last thing we select , so suppose we select abc.txt and then we try to close the node by clicking its icon which close the node
then this obj fetches " null " because tree selection event is fired and when we close the node it try to fetch what we have selected but couldnt find anything except null

so , simple way to handle this is try catch , catch will catch(NullPointerException npe) , but when i did that it doesnt work , the code inside the catch is not printed on the console and again exception is thrown like i have given you above
so can you suggest me any simple solution to handle this ?
let me give you lines which are above this
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
What’s this about null leaf nodes? Have you got any null leaf nodes?

Look at this treeBut that isn’t how a binary tree works at all. You don’t have different node and leaf classes. Your leaves are nodes which don’t have children, but they still have pointers to those children, so they will point to null.
That means: Every binary tree is guaranteed to contain null pointers. In fact, I think you can work out how many nulls there are: I think it is n + 1 where n is the number of nodes.
Now I know a displayed tree is rather different from a binary tree, but you need to check
  • Do your leaf nodes have null pointers to their supposed children?
  • Similarly to their user Objects?
  • Should you be returning null if you pick up one of those leaves,
  • or should you throw an Exception, to be caught higher up the stack,
  • or should you do something different?
  • Have you set a user Object, or put one there by any other means?

  • And what happened when you printed my is not null test from earlier (line 807½)?

    Not sure I can go much rather. Sorry, I have probably suggested all I can. You will have to look harder for that null.
    Anybody else, please?
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    Campbell Ritchie wrote:What’s this about null leaf nodes? Have you got any null leaf nodes?

    Look at this treeBut that isn’t how a binary tree works at all. You don’t have different node and leaf classes. Your leaves are nodes which don’t have children, but they still have pointers to those children, so they will point to null.
    That means: Every binary tree is guaranteed to contain null pointers. In fact, I think you can work out how many nulls there are: I think it is n + 1 where n is the number of nodes.
    Now I know a displayed tree is rather different from a binary tree, but you need to check
  • Do your leaf nodes have null pointers to their supposed children?
  • Similarly to their user Objects?
  • Should you be returning null if you pick up one of those leaves,
  • or should you throw an Exception, to be caught higher up the stack,
  • or should you do something different?
  • Have you set a user Object, or put one there by any other means?

  • And what happened when you printed my is not null test from earlier (line 807½)?

    Not sure I can go much rather. Sorry, I have probably suggested all I can. You will have to look harder for that null.
    Anybody else, please?

    thanks for trying , and by the way ....at line 807 1/2 it prints out to be null
    i know the reason ...but i m not getting any way to fix this
    that try & catch thing is not working in this case , because null pointer in not checked exception
    k fine thanks once again for trying i will look around
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18991
        
        8

    Come on, this isn't rocket science. Really. The simple solution is

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    At least we know it’s null, which means Paul C’s suggestion shouldn’t have taken more than a few seconds to work out. But the try-catch will work just as well for an unchecked Exception.
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    Paul Clapham wrote:Come on, this isn't rocket science. Really. The simple solution is


    thanks paul , after looking at the solution , i definetely laughed at me ,
    because i cant figure out this tiny bug
    anyways thanks to champbell and you
    thank you very much
    and i agree this isnt a rocket science ....lolz
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40052
        
      28
    You’re welcome
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: null pointer exception while closing a node of jtree