Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Helper Methods and Conditionals

Two Errors after compiling, why?

public class Example {

public static void main(String[] args) {
    // Your amazing code goes here...

  System.out.println("We are making a new Pez Dispenser.");
  PezDispenser dispenser = new PezDispenser("Yoda");
  System.out.printf("The dispenser character is %s\n", 
                      dispenser.mCharacterName);

  if (dispenser.isEmpty()){
      System.out.println("It is currently empty");
    }      
    System.out.println("Loading.....");      
    dispenser.load();

    if (!dispenser.isEmpty()){
      System.out.println("It is no longer empty");
    }        
}

}

This is the Error i get. Example.java:11: error: cannot find symbol
if (dispenser.isEmpty()){
^
symbol: method isEmpty()
location: variable dispenser of type PezDispenser
Example.java:17: error: cannot find symbol
if (!dispenser.isEmpty()){
^
symbol: method isEmpty()
location: variable dispenser of type PezDispenser
2 errors

public class PezDispenser{ public String mCharacterName;

public static final int MAX_PEZ = 12;

private int mPezCount;

//This is Constructer it has the same name as class //mCharacter is member character public PezDispenser(String characterName){ mCharacterName = characterName; mPezCount = 0; } public String getCharacterName(){ return mCharacterName; } public boolean isEmtpy(){ return mPezCount == 0; } public void load(){ mPezCount = MAX_PEZ; } public boolean dispense(){ boolean wasDispensed = false; if(!isEmpty()){ mPezCount--; wasDispensed = true; }
return wasDispensed; }

}

2 Answers

Benjamin Gooch
Benjamin Gooch
20,367 Points

I'm not 100% sure because I didn't try running this, but I noticed that you are trying to reference "dispenser.mCharacterName" directly in your System.out.printf instead of calling the method used to get that variable. Intead of

System.out.printf("The dispenser character is %s\n", 
                      dispenser.mCharacterName);

you should call the method like this:

System.out.printf("The dispenser character is %s\n", 
                           dispenser.getCharacterName());

It's getCharacterName()'s job to return the value of mCharacterName. mCharacterName is a private variable and therefor the compiler can't read it because of it's restricted access.

That may fix the issue. Also, if that doesn't work, could you repost the code for the second file you included? It's pretty much unreadable and I had to copy and paste it into workspaces and reformat it in order to provide assistance. It just makes it easier for people if you help them out.

EDIT: I just noticed you have a typo on the word "Empty". In your declaration of isEmpty() you typed isEmtpy().

THAT should fix your problem.

Matthew Magee
Matthew Magee
8,772 Points

Could you show me what your other class looks like containing the isEmpty() method?