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

Keep getting cannot find symbol error

I'm trying to create a player class but keep getting cannot find symbol in my main and in my player class.

my main public class Introductions {

public static void main(String[] args) {
    Console console = System.console();
    // Welcome to the Introductions program!  Your code goes below here
    System.out.println("Welcome to Food Fight!!!");
    Player player = new Player();
    System.out.printf("%s is very delicous $n",
                      player.playerName());

} }

i get an error: Introductions.java:11: error: cannot find symbol
player.playerName());

as well as in my player class. I'm not sure what i'm doing wrong here,

public class Player { private String playerName;

public Player() { playername = name; }

public Player(String playerName) {

this.cheesecake = "Cheesecake";
this.benihana   =  "Benihana";
this.mellowMushroom =  "Mellow Mushroom";
this.fingerLickin =  "Finger Lickin";
this.rusticInn =  "Rustic Inn";

}

//This gets method or getter public String getPlayerName() { return playerName;

}

}

6 Answers

Hi Joel,

Reviewing your code, I'm seeing three critical issues and one potential bug.

The first issue is that you're trying to call the private property playerName() as a method - you probably meant to call getPlayerName() there?

The second and third issues (and the bug) are in your two constructors for the Player class. Without passing an argument, Player assigns name to the playerName property, but name isn't declared anywhere in the code, so you're assigning a null pointer. If you pass Player a string argument, it doesn't use that string for anything, and instead assigns String values to a number of Player properties which haven't been declared as part of the Player object.

Hope that helps!

Thanks Evan for your help...

Happy to help! Also, you may wish to consider using GitHub for version control on your project, as it has the added benefit of showing others what you're able to do and making it easy to share code when you need help.

Thanks Evan for replying I appreciate your help and advice. I created another constructor with nothing passing thru and set a default player name. I'm still trying to pass those multiple properties. I'm still having hard time understanding passing a string.

public Player(String name) { playerName = name;
}

when i run in the main public class Introductions {

public static void main(String[] args) {
    Console console = System.console();
    // Welcome to the Introductions program!  Your code goes below here
    System.out.println("Welcome to Food Fight!!!");
    Player player = new Player(String name);
    System.out.printf("%s is very delicous $n",
                      player.getPlayerName());

} }

i get this error

public class Introductions {

public static void main(String[] args) {
    Console console = System.console();
    // Welcome to the Introductions program!  Your code goes below here
    System.out.println("Welcome to Food Fight!!!");
    Player player = new Player(String name);
    System.out.printf("%s is very delicous $n",
                      player.getPlayerName());

} }

My question is do i need to declare the other properties just as i did with playerName but set it equal to that name like private String playerName = Burger King; or would it just be private String BurgerKing; I'm trying to get the program to print out the all those named properties. Also from your answer I assume once the properties are declared i can just leave them in the constructor that passes string. I hope i didn't confuse you i'm still trying to get the lingo down. Thanks for your help.

Introductions.java:9: error: ')' expected
Player player = new Player(String name);
^
Introductions.java:9: error: illegal start of expression
Player player = new Player(String name);

Sorry this is the error i meant.

I'm not certain what the instructions to this were, so I'm guessing a bit, but... I think you're trying to do something like this?

public class Introductions {
    public static void main(String[] args) {
            Console console = System.console();
            System.out.println("Welcome to Food Fight!!!");
            Player player = new Player();
            System.out.printf("%s is very delicous $n",
                      player.getPlayerName());
    } 
}

public class Player { 
    private String playerName;
    //Making these strings public as they have no getters, setters or methods that use them
    public String cheesecake = "Cheesecake", benihana = "Benihana",
            mellowMushroom =  "Mellow Mushroom", fingerLickin =  "Finger Lickin",
            rusticInn =  "Rustic Inn";

    public Player() { 
        playername = "Unnamed person"; 
    }

    public Player(String name) {
        playerName = name;
    }

    public String getPlayerName() { 
        return playerName;
    }
}

Thank you, I'm still new to this. You just cleared up so much about the multiple strings as well as future questions about setters and getters.

This was just something i wanted to work on to see how to have just a list of players already in the player class rather than just one, if that makes sense

It makes sense, you may want to have the list be an actual list variable though. I think you could even get away with something like this if you wanted, although I haven't ran it through a compiler to see how my syntax is. Pre-incrementing returns the value though I believe, and remove() also returns the value of the removed index, so I believe the ternary would suit your needs.

List playerNames = new ArrayList(["Cheesecake", "Benihana",
            "Mellow Mushroom", "Finger Lickin",
            "Rustic Inn"]);
int unknownPlayerCount = 0;

public class Player { 
    private String playerName;

    public Player() { 
        playername = 
            playerNames.length > 0 ? playerNames.remove(0) : ("unknownPlayer" + (++unknownPlayerCount)); 
    }

    public Player(String name) {
        playerName = name;
    }

    public String getPlayerName() { 
        return playerName;
    }
}