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 Incrementing and Decrementing

Edmond Gottstein
Edmond Gottstein
452 Points

Main method not found?

I get this error in the workspace and dont know what to do:

Error: Main method not found in class PezDispenser, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

This is the code:

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.getCharacterName());

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

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

}

}

AND :

public class PezDispenser {

public static final int MAX_PEZ = 12;

private String mCharacterName; private int mPezCount;

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

public boolean isEmpty(){ return mPezCount == 0; }

public void load() { mPezCount = MAX_PEZ; }

public String getCharacterName(){ return mCharacterName; }

}

I didn't change anything, in previous video this code worked well, and after without changing anything i get this error.

Hi Edmond Gottstein,

It would be helpful if you could show us your code.


Thanks, ISAIAH S

1 Answer

Tim Collins
Tim Collins
5,353 Points

Hi Edmond. I was having the same problem. In my case it was because I was trying to compile the PezDispenser.java rather than the Example.java.

Regards.

Tim.

I was having the same problem. Thank you for you answer!