• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with accessiong variables defined in another package  RSS feed

 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have to access a variable defined in another package

i have two classes .both are in deifferent packages
in TestPanel2.java i have a variable s.
i have to access this variable in parsing.java defined in another package
in parsing.java i have written like this
TestPanel2 t= new TestPanel2();
System.out.println(t.s)
it is printing the value as null.
i have imported that class
i have set the classpath also
please tell me why null is displaying
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the variable is declared as public, and you have set a value to it, it shouldn't be returning null, the only reason why it would be returning null is because you haven't set any value to it yet.
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it has the value assigned in TestPanel2.java.actually that value iam retreiving from textfield

String s= tf.getText();
it has some value in it.
now in parsing.java which is in another package
TestPanel2 t= new TestPanel2();
System.out.println(t.s);
it is printing as null
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to stop and think about what is happening. You have a panel containing a text field and elsewhere in your code, you want to processs the text after the user has entered it. The key word is "after". Do you know how to react to text being entered? And *what* do you expect will trigger the reaction -- the user pressing a certain button, typing <enter> or something else?
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is that you are not accesing the variable you want, if you have a class where you give a value to a variable, and then you want to acces it, you have to get the right adress, what I mean is that if you have already initialized the variable means that an instance of your class has been created, so if you write TestPanel2 t= new TestPanel2(); you are acessing the variable s of the new TestPanel not the one where you initialized the variable. Hope this helps.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you plese post some more complete code. For example, you need to post the full method in the TestPanel2 class where you get the text from the text field. Also post the method in the parsing class (you should name it Parsing instead) where you get the value from the TestPanel2 class. If appropriate, you also need to show us where you create any TestPanel2 objects as well as any other objects that are might illustrate what happens.

I say this because I wonder if you understand variable scoping. I suspect that you are not really accessing the variables you think you are because of scope issues. However, I cannot tell for sure until I see your code.

Layne
 
pvsr rao
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Emili Calonge
I have understand the problem now
 
Emili Calonge
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, I'm glad I helped you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!