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

Android

not sure why this doesnt work

here is the code

public class Example {

    public static void main(String[] args) {
        // Your amazing code goes here...
        System.out.println("We are making new containers");
        Containers Containers = new Containers();
        System.out.println("The container character is %s\n", mCharacterName);
    }
}
public class Container {
  public String mCharacterName = "Mickey";
}

here is the error message

./Containers.java:1: error: class Container is public, should be declared in a file named Container.java                                    
public class Container {                                                                                                                    
       ^                                                                                                                                    
Example.java:6: error: cannot access Containers                                                                                             
        Containers Containers = new Containers();                                                                                           
        ^                                                                                                                                   
  bad source file: ./Containers.java                                                                                                        
    file does not contain class Containers                                                                                                  
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.                                                    
2 errors                  

i dont get why i do everything the teacher does and get an error when he doesnt. anyone else run into this problem? note for the exercise it was PezDispenser but i changed it to Container.

it was a scope problem and i basically had to declare the Container class in the example file. what i DONT understand is why craig doesnt have to declare it in the video and it works for him... but i have to declare it myself

Well apart from your error, I think you can only access mCharacterName string variable by calling the object instance first, like this: Containers.mCharacterName. also, try to use a diferente name for the instanciated object, not equal to the class. eg: Containers mContainers = new Containers (); or at least low case, Containers containers = new Containers ();

3 Answers

it wasnt working for me due to a scope error i believe its called. i had to declare container item outside of the braces for it to work.

what i still dont understand is how i have to do that yet in the video the instructor doesnt have to and his code still works

Dean Onizuka i was looking at your code i think you spelled it wrong when u were creating the object ... U created a class name Container but when making a object u spelled it Containers .... u added a extra character s .... See if this works :)

In addition to using 'Container' to create a new Container object, you cannot use a variable with the same exact case, i.e. 'Container'. Your declaration should probably look like this.

Note the lower case 'container' variable.

public class Example {

    public static void main(String[] args) {
        // Your amazing code goes here...
        System.out.println("We are making new containers");
        Container container = new Container();
        System.out.println("The container character is %s\n", mCharacterName);
    }
}