• Post Reply Bookmark Topic Watch Topic
  • New Topic

Should this work in theory?  RSS feed

 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a project similar to this. I have a globally declared String called aHostIP in Class1 and I want ipAdd in Class2 to equal aHostIP. After buildGui method runs, aHostIP equals "0.0.0.0" . However, when Class2 calls the getHostIP method, I get a aHostIP with a null value, but if I put in a value in the getHostIP method, like aHostIP = "0.0.0.0"; there is no trouble. Besides the fact that main()is in the code. Shouldn't ipAdd = "0.0.0.0"? Is the getHostIP method redeclaring aHostIP and nullifying it? If so, how can I get the aHostIP value from buildGui() to ipAdd in Class2? Thanks for any help!
CLASS 1

CLASS 2
 
Billybob Marshall
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tyler Jordan:
Class1 hostip = new Class1;
I'll assume you meant new Class1();
Ok, so now you've constructed a Class1 object. Its default constructor was
invoked, leaving aHostIP = null
String ipAdd = hostip.getHostIP();
System.out.println(ipAdd); <-**returns null**

Of course. You never called buildGui(), so what do you expect? Do you think 'buildGui' is a magic method that should automatically get called?
 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad, assuming buildGui() was called by main().
 
Billybob Marshall
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there still a problem then? I don't know what your last post means (you goofed the post and actually had called buildGui, or what?)
If you had called hostip.buildGui() just before calling hostip.getHostIP(), then it would have definitely returned "0.0.0.0".
 
Tyler Jordan
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I called buildGui through main() to set the variable. Then, Class2 calls the getHostIP() from Class1. Maybe I am creating two instances of Example1 object??
 
Antoine Waugh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heyas,

>>I have a globally declared String called aHostIP in Class1

well firstly this is incorrect. you have failed to make an access modifier (public/private) so you can assume it to be a 'friendly' variable. which means any class in the same package (or subclass in the same package) has the right to access the string -aHostIP

>>and I want ipAdd in Class2 to equal aHostIP. After buildGui method runs, aHostIP equals "0.0.0.0" . However, when Class2 calls the getHostIP method, I get a aHostIP with a null value, but if I put in a value in the getHostIP method, like aHostIP = "0.0.0.0"; there is no trouble.

i think you should go back a little. firstly you need to determine where your main method is going to be defined. secondly if you want ANYTHING defined on object construction, you place it in the constructor (ie it looks like buildGUI should just be a constructor call..)if all you want is an instance of a class to get the string value from another then use the following:

Currently i think the following need addressing:
1. >>public String runOper(String s)
you accept a parameter 's' but never use it..
2. your class names are Class1 and Class2, maybe think of a more meaningful name
3. dont presume that your class1 ahostIP will always be 0.0.0.0
taking these into account, this is what i think is more on track..


hth
-twans
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!