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

Michael Stolp-Smith
Michael Stolp-Smith
4,855 Points

Getting two errors while compiling and I am not sure why. Syntax seems correct.

So far I have 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);
}

}

which as far as I know should be working. The concepts here make sense, but I am getting a compiler error along the PezDispenser dispenser = new PezDispenser(); indicating a symbol not found on "PezDispenser" and "PezDispenser();"

if anyone could explain this to me that would be great.

Blake Larson
Blake Larson
12,989 Points

Could you add your PezDispenser class as well. You main method looks right.

5 Answers

Michael Stolp-Smith
Michael Stolp-Smith
4,855 Points

Answer: I am dumb and capitalized the file extension (PezDispenser.Java as opposed to PezDispenser.java)

Hello

I believe your error is here:

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

mCharacterName is generally a private attribute in a Class. To get ti it, you should be using a getter method instead. Please double check you class and use a gerrter method. It should be something like

dispenser.getCharacterName()

if this answers your question, please mark the question as answered.

Thanks

Michael Stolp-Smith
Michael Stolp-Smith
4,855 Points

I made this change in the document and it did not solve the issue.

As far as I can tell my syntax matches everything in the video and I am still getting compiler errors of "cannot find symbol". That line is also not indicated as an error line, and the video does make use of dispenser.mCharacterName and not dispenser.getCharacterName(). The errors come before that as well, so that seems to be unrelated.

Michael Stolp-Smith
Michael Stolp-Smith
4,855 Points

note that this is the first exercise and this should work using mCharacterName

Michael Stolp-Smith
Michael Stolp-Smith
4,855 Points

Alright, this is yet solved perfectly though I did work around it (with help from a friend) by adding the PezDispenser class directly to Example in one document, and also made PezDispenser static.

{

static class PezDispenser { private String mCharacterName;

public PezDispenser(String characterName){
      mCharacterName = characterName;
}

public String getCharacterName(){
    return mCharacterName;
}

} }

I am yet unsure why this doesn't work in the workspace across two files.