![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
please help in solving: Exception in thread "main" java.lang.nullpointerexceptionhi all,
i'm using the following code and i'm getting exception in thread "main" java.lang.nullpointerexception at the line which is in bold. JAVA Code:
even though i create 'tree1' in populateTree(), it seems that 'tree1' is again null in the bold line. can anyone help me please... Last edited by LuciWiz : 08-May-2006 at 07:12.
Reason: Please insert your Java code between [java] & [/java] tags
|
|
#2
|
||||
|
||||
Re: please help in solving: Exception in thread "main" java.lang.nullpointerexceptionThe constructor of the ProjectDemo class will be called before the function populateTree (which I don't even know where in the code you will call). So the tree1 member will be not instantiated, hence the "null pointer" exception.
I suggest you move that piece of code from the function body in the constructor, before using tree1. Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
|||
|
|||
Re: please help in solving: Exception in thread "main" java.lang.nullpointerexceptionas u can see in the code, i'm calling populateTree() in the connectDlg() which triggers when user connects to database.
also i have just declared 'tree1', and all the initialization and adding of root and childs is done in populateTree(). after doing this only the 'tree1' becomes visible. now as i think the treeselectionListener will be triggered only after populateTree() executes, but why do i get the nullPointerException ? |
|
#4
|
||||
|
||||
Re: please help in solving: Exception in thread "main" java.lang.nullpointerexceptionQuote:
I studied Java some time ago, and I never was a "master" in this programming language, therefore I may be terrible wrong. However, it seems to me that you will not be able to call a non-static function of the ProjectDemo class (like connectDlg), and will need on instance of it for that. You actually instantiate the class in main: Quote:
This is where the constructor will be called - before the call to the connectDlg function, IMHO. I respectfully suggest you try my previous suggestion, unless someone else offers more insightful help. Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#5
|
||||
|
||||
Re: please help in solving: Exception in thread "main" java.lang.nullpointerexceptionQuote:
If your code sample is the whole example, then you first of all should change the visibility of this JTree to private (as is good practice in any class... unless you need it otherwise). Secondly, though you have declared the variable... that doesn't mean it is instantiated. You need something like this at least... JAVA Code:
Let me know if that works. __________________
If practice makes perfect and nobody's perfect... why practice? Homepage: http://www.treywhite.com Blog: http://www.treywhite.com/blog.php Web Design Company: http://www.ewebproductions.com |
|
#6
|
||||
|
||||
Re: please help in solving: Exception in thread "main" java.lang.nullpointerexceptionQuote:
Actually, he is doing that, just not in the right place, I argue. __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#7
|
||||
|
||||
Re: please help in solving: Exception in thread "main" java.lang.nullpointerexceptionAh yes... good call. Then you are correct in that any use of the tree1 object before it is instantiated will cause a nullpointerexception... and since the tree (according to the poster) is being initialized in the populateTree() function... any use of the object before that will throw this exception.
So, the bolded line in the constructor will always be called before the populateTree() function causing this error. I overlooked that ... sorry It looks like a redesign is needed. You need to initialize the tree in the constructor OR create a function that creates the TreeSelectionListener and make sure it is called after the tree is populated. __________________
If practice makes perfect and nobody's perfect... why practice? Homepage: http://www.treywhite.com Blog: http://www.treywhite.com/blog.php Web Design Company: http://www.ewebproductions.com |
|
#8
|
|||
|
|||
Re: please help in solving: Exception in thread "main" java.lang.nullpointerexceptionThank u for the suggestion u made. i have solved my problem by using DefaultTreeModel.
Anyway i would like to ask one more thing, i.e., if i initialize the tree as u said, i.e., JAVA Code:
|
|
#9
|
||||
|
||||
Re: please help in solving: Exception in thread "main" java.lang.nullpointerexceptionAn object assignment is just like a variable assignment. If you create two integers, and you want to assign one integer the value of the other integer, it would go like this:
JAVA Code:
Similarly, you do the same for objects: JAVA Code:
It's that simple. __________________
If practice makes perfect and nobody's perfect... why practice? Homepage: http://www.treywhite.com Blog: http://www.treywhite.com/blog.php Web Design Company: http://www.ewebproductions.com |
Recent GIDBlog
Stupid Management Policies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Monitoring Thread Size | mark343 | MS Visual C++ / MFC Forum | 0 | 04-May-2006 10:09 |
| Unable to catch exception in native code | QED | C++ Forum | 1 | 25-Jan-2006 14:12 |
| C++ Copy Constructor & Exception issues | greymalkin | C++ Forum | 3 | 05-Oct-2005 22:36 |
| Sending message to GUI thread that ISNT the main thread | mpviii | MS Visual C++ / MFC Forum | 1 | 20-Apr-2005 13:49 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The