Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Meet Objects Creating Classes

Cant figure this one out...

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();
    System.out.printf("The dispenser character is %s\n",
                      dispenser.mCharacterName);
}

}

Error keeps referring to the following line: PezDispenser dispenser = new PezDispenser();

Any ideas?

Im sorry...I guess I don't follow what your saying....I just did everything that he did...or at least I thought, just trying to figure out what doesn't matchup

Seth Kroger
Seth Kroger
56,403 Points

What is your PezDispenser.java code?

It says the error is in this line of code Keeps having an arrow pointing to the capital P in "PezDispenser();

PezDispenser dispenser = new PezDispenser();

Says cannot find Symbol..yet im typing it exactly as shown in video..

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();
    System.out.printf("the dispenser character is %s\n",
                      dispenser.mCharacterName);
}

}

there is my code....

Seth Kroger
Seth Kroger
56,403 Points

You're supposed to have a 2nd file called PezDispenser.java that defines and implements the PezDispenser class. If you don't have it that's probably what's causing the trouble.

PezDispenser.Java

public class PezDispenser { public String mCharacterName - "Yoda"; }

pezdispenser.java

I got it finally! lol

3 Answers

Seth Kroger
Seth Kroger
56,403 Points

The constructor for PezDispenser is supposed to be passed a String for the name of the character. You're getting that error because there is a mismatch in argument types.

Seth Kroger
Seth Kroger
56,403 Points

OK, you're a bit earlier along than I thought. The issue is the statement:

  public String mCharacterName - "Yoda";

The minus sign should be an equals.

I fixed that but im still getting the same error message.....

public class PezDispenser { public String mCharacterName = "Yoda"; }

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();
    System.out.printf("the dispenser character is %s\n",
                      dispenser.mCharacterName);
}

}

Seth Kroger
Seth Kroger
56,403 Points

Is the file for PezDispenser named "PezDispenser.java" or "PezDispenser.Java"? (the first is correct, the 2nd isn't)

Oscar Barrascouth
Oscar Barrascouth
19,233 Points

I am getting the same error in the console. Any solutions? Both of my files are Example.java and PezDispenser.java.

Example.java code:

public class Example {

public static void main(String[] args) {
    // Your amazing code goes here...
  System.out.println("We are making a Pez Dispenser.");
  PezDispenser dispenser = new PezDispenser();
  System.out.printf("The dispenser character is %s\n",
                    dispenser.mcharacterName);  

}

}

PezDispenser.java code:

public class Example { public String mcharacterName = "Yoda"; }

Console error:

./PezDispenser.java:1: error: class Example is public, should be declared in a file named Example.java
public class Example {
^
Example.java:6: error: cannot access PezDispenser
PezDispenser dispenser = new PezDispenser();
^
bad source file: ./PezDispenser.java
file does not contain class PezDispenser
Please remove or make sure it appears in the correct subdirectory of the sourcepath.
2 errors

Nicolás Lavín
Nicolás Lavín
1,922 Points

yep, you have to put public before class PezDispenser {}