GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-May-2006, 06:37
vkiran_v vkiran_v is offline
New Member
 
Join Date: May 2006
Posts: 14
vkiran_v is on a distinguished road

please help in solving: Exception in thread "main" java.lang.nullpointerexception


hi 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:
public class ProjectDemo
{	
	JTree tree1;
	public ProjectDemo(String title)
	{
		//MENU CREATION CODE HERE
		menuConnect.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{ connectDlg(); }
		});
	       [b]tree1.addTreeSelectionListener(new TreeSelectionListener()[/b]
		{
			public void valueChanged(TreeSelectionEvent e) 
			{//SOME CODE HERE}
		});
	}
	public void connectDlg()
	{
		//SOME DIALOG BOX WITH CONNECT BUTTON
		connButton.addActionListener( new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{ populateTree(userTxt.getText()); }
		});
	}
	public void populateTree(String user)
	{//TREE CREATION CODE HERE}
	public static void main(String[] args)
	{ProjectDemo projectDemo = new ProjectDemo("SOLAP Browser");}
}

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  
Old 08-May-2006, 07:23
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: please help in solving: Exception in thread "main" java.lang.nullpointerexception


The 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  
Old 08-May-2006, 14:02
vkiran_v vkiran_v is offline
New Member
 
Join Date: May 2006
Posts: 14
vkiran_v is on a distinguished road

Re: please help in solving: Exception in thread "main" java.lang.nullpointerexception


as 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  
Old 08-May-2006, 14:40
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: please help in solving: Exception in thread "main" java.lang.nullpointerexception


Quote:
Originally Posted by vkiran_v
as 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 ?

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:
Originally Posted by vkiran_v
JAVA Code:
public static void main(String[] args)
	{ProjectDemo projectDemo = new ProjectDemo("SOLAP Browser");}

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  
Old 08-May-2006, 15:28
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: please help in solving: Exception in thread "main" java.lang.nullpointerexception


Quote:
Originally Posted by vkiran_v
JAVA Code:
	JTree tree1;

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:
	JTree tree1 = new JTree();

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  
Old 08-May-2006, 16:02
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: please help in solving: Exception in thread "main" java.lang.nullpointerexception


Quote:
Originally Posted by TreyAU21
Secondly, though you have declared the variable... that doesn't mean it is instantiated. You need something like this at least...

JAVA Code:
	JTree tree1 = new JTree();

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  
Old 09-May-2006, 00:27
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: please help in solving: Exception in thread "main" java.lang.nullpointerexception


Ah 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  
Old 09-May-2006, 04:59
vkiran_v vkiran_v is offline
New Member
 
Join Date: May 2006
Posts: 14
vkiran_v is on a distinguished road

Re: please help in solving: Exception in thread "main" java.lang.nullpointerexception


Thank 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:
JTree tree1 = new JTree();
then a sample tree is coming up. at a later moment how can i assign a new tree i have created to the 'tree1'?
  #9  
Old 09-May-2006, 08:06
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: please help in solving: Exception in thread "main" java.lang.nullpointerexception


An 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:
int x;
int y=4;

x=y; //<---

Similarly, you do the same for objects:

JAVA Code:
Object x;
Object y;

// Do some initialization and manupulation of y and then...

x=y;

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 GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 12:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.