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 Meet Objects Final

What does it mean? Exception in thread "main" java.lang.NoSuchMethodError: Pez.Dispenser.sweapHead(Ljava/lang/String;)

PezDispenser.java

class PezDispenser {
private String characterName;


public PezDispenser(String characterName);
  {
  this.characterName = characterName;
}

public String getCharacterName();
 { 
  return characterName;
}

public String swapHead(String characterName)
  {
  String originalCharacterName = this.characterName;
  this.characterName = characterName;
  return originalCharacterName;

}

}

Example.java

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 is %s %n",
                      dispenser.getCharacterName()
                     );
                  String before = dispenser.swapHead("Darth Vader");
                  System.out.printf("It was %s but Chris switched it to %s %n",
                                  before,
                                  dispenser.getCharacterName()
                                  ); 


  }


}

Moderator edited: Added markdown so the code renders properly in the forums.

3 Answers

Christopher Augg
Christopher Augg
21,223 Points

Stefan,

It looks like you just have some typos with the ending semicolons.

public PezDispenser(String characterName);  // <---remove semicolon 
  {
  this.characterName = characterName;
}

public String getCharacterName(); // <-- remove semicolon
 { 
  return characterName;
}

Thank you. I corrected these mistakes but i get the same error as bevor...

Christopher Augg
Christopher Augg
21,223 Points

sorry to hear that. hmm... the error says Pez.Dispenser instead of PezDispenser and and sweapHead instead of swapHead. Do you have some other typos in filenames etc? I can take a look at your workspace if you would not mind forking it.

I just used your code within my workspace with the corrections and it works fine.

Here is a snapshot you can fork.

Using:

clear && javac Example.java && java Example

compiles and runs it.

Ok thank you Chris, now it works. I think that my editor CodeRunner had a problem with this Code.

Best regards