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 Methods and Constants

CD Lim
CD Lim
4,782 Points

PezDispenser dispenser = new PezDispenser ("Yoda") return java.util.NoSuchElementException

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; // mPezCount set to maximum number of pez }

public String getCharacterName() { return mCharacterName; } }

Will add formatting for easier viewing (check the Markdown Cheatsheet)

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; 
        // mPezCount set to maximum number of pez 
    }

    public String getCharacterName() { 
        return mCharacterName; 
    } 
}

My thoughts: your 'load' method needs perenthesis:

public void load() {

The "java.util.NoSuchElementException" error should identify the specific element and type it is dealing with? What does it say?

CD Lim
CD Lim
4,782 Points

hey it works, thank you very much clumsy me... haha

1 Answer

Steve Brewer
Steve Brewer
15,030 Points

For anyone else who might be having this "java.util.NoSuchElementException" error, I just had the same thing. The error gave no other information.

I realised maybe I needed to compile Example.java, so I did and it threw an error relating to the isActuallyEmpty variable I created in a later exercise (I've gone back a bit after getting confused). Once I commented that out, it compiled fine and it solved the NoSuchElementException error.

So check your code is compiled and that there are no other errors.