• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using variables from other classes  RSS feed

 
Edmund Cornforth
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been working my way through Head First Java and got to the guessgame part, which all went smoothly without a hitch. So I thoughts to myself, "why not play around a bit with this to get my head round how all these classes interact with each other?" I thought I would try and use the phrase-o-matic class created in a previous chapter to provide my players with a randomly generated phrase they could exclaim when they won or lost the game. I couldn't obtain the values though.

Here's the modified phrase generator:



And here's the modified player class:



And here's a mini class I created just to test whether it would return values before putting it into the guessgame class:



They all compile but when I run testrun I get this output:

I'm gonna guess 4
Exception in thread "main" java.lang.NoSuchMethodError: player.shout()Ljava/lang/String;
at testrun.main(testrun.java:5)


Am I doing something wrong? What kinds of questions should I be even asking?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edmund Cornforth wrote:
They all compile but when I run testrun I get this output:

I'm gonna guess 4
Exception in thread "main" java.lang.NoSuchMethodError: player.shout()Ljava/lang/String;
at testrun.main(testrun.java:5)


Am I doing something wrong? What kinds of questions should I be even asking?


You probably need to recompile everything -- specifically, your testrun class. This class is expecting the player class' shout() method that returns a string, and you probably recently changed it (and compiled) to be a void method.

Henry
 
Edmund Cornforth
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! That definitely solved that error, but now it returns this when I run it:

"I'm gonna guess 6
Player says: null"
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Take a looks at line 13 of the insultomatic class. And question, do you know the difference between an instance and local variable?
 
Edmund Cornforth
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works!

I got rid of say "String" in front of i1 on line 13 and that seemed to sort it! By saying "String" in front of it did that create a new local variable, whereas by removing it I defined the instance variable I declared at the top by saying "String i1;"?

Am I thinking in the right direction or is that completely wrong?
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You declared two variables named 'i1'. You had an instance variable (aka 'field') and another variable local to the method. In the method the local variable 'i1' was used because you didn't prefix it with 'this' (as in this.i1). This by itself is not necessarily the problem because the local variable was returned by the method. However, when you called insult() you ignored the returned string (ie threw it away). Then you tried to access the field, which had not been set, and didn't get the result you were looking for.

Solution is to have a field or a local variable but not both (which is obviously confusing). To use the field, remove the word "String" from the local variable declaration. To use the local variable, remove the field entirely and then assign the returned String from insult() to another String variable (or use the return directly).
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!