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

PezDispenser dispenser = new PezDispenser("Yoda");

Why would this error come up:

java.util.NoSuchElementException

I did not change the code from the previous section but I could not do the Methods and Commands workspace activity. :(

Could I see your code?

3 Answers

Andrea Miotto
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andrea Miotto
iOS Development Techdegree Graduate 23,357 Points

java.util.NoSuchElementException is a RuntimeException which can be thrown by different classes in Java like Iterator, Enumerator, Scanner or StringTokenizer. All of those classes has method to fetch next element or next tokens if underlying data-structure doesn't have any element Java throws "java.util.NoSuchElementException".

Anyway if you would like to show us your code, maybe we can help you.

  public class PezDispenser {

  public static final int String mCharacterName;

  public final int MAX_PEZ = 12; 

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

  public String getCharacterName() {
    return mCharacterName;
  }
}

I am getting the same thing, java.util.NoSuchElementException

Kelly Hughes
Kelly Hughes
2,444 Points

Hi Kamran,

I believe that there's some error in your second line of code: public static final int String mCharacterName;

  • a variable cannot have two datatypes (int String) -the (public static final) portion is telling the compiler that the variable value will not change, but in this case, mCharacterName can change with each object.

Perhaps the code should be:

public static final int MAX_PEZ = 12;

String mCharacterName;

I hope that this helps :)