-1

I'm pretty knew to Java so I don't quite understand what I'm looking for when I'm trying to find errors in my program. I'm trying to create a player class for a game and I'm getting the "cannot find symbol" error. I did some research prior to asking this question but I don't understand the responses.

I get errors such as:

PlayerClass.java:51: cannot find symbol
symbol  : variable getLevel
location: class PlayerClass
    PlayerClass.setLevel(PlayerClass.getLevel + 1);
                                    ^
PlayerClass.java:51: setLevel(int) in PlayerClass cannot be applied to     (<nulltype>)
    PlayerClass.setLevel(PlayerClass.getLevel + 1);
               ^
PlayerClass.java:93: cannot find symbol
symbol  : variable getName
location: class PlayerClass
    if (name.equals(PlayerClass.getName))
                               ^
3 errors

But here's the class:

import io.*;
//CLASS: Player
public class PlayerClass
{
//CLASS FIELDS: name, level, damage, health, kills

//CONSTRUCTORS:
private static int level, health, kills, damage;
private static String name;
//Default Constructor:  IMPORT: None
public PlayerClass()
{
    name = "Jeremy";
    level = 1;
    health = 100;
    kills = 0;
}

//Alternate Constructor:     IMPORT: inName
public PlayerClass(String inName)
{
    name = inName;
    level = 1;
    health = 100;
    kills = 0;
}

//Copy Constructor:  IMPORT: inPlayer
public PlayerClass(PlayerClass inPlayer)
{
    name = inPlayer.getName();
}   

//MUTATORS:

//setName   IMPORT: inName
public void setName(PlayerClass inPlayer)
{
    name = inPlayer.getName();
}

//setLevel  IMPORT: inLevel
public int setLevel(int inLevel)
{
    PlayerClass.level = inLevel;
}

//levelUp       IMPORT:inPlayer
public int levelUP(PlayerClass inPlayer)
{
    PlayerClass.setLevel(PlayerClass.getLevel + 1);
}

//ACCESSORS:

//getName   IMPORT: None    EXPORT: name
public String getName()
{
    return name;
}

//getLevel  IMPORT: None    EXPORT: level
public int getLevel()
{
    return level;
}

//getHealth IMPORT: None    EXPORT: health
public int getHealth()
{
    return health;
}

//getKills      IMPORT: None    EXPORT: kills
public int getKills()
{
    return kills;
}

//toString      IMPORT: None    EXPORT: playerStr
public String toString()
{
    String playerStr;
    playerStr = "Name: " + name + "Level: " + level + "Kills: " + kills;
    return playerStr;
}

//equals        IMPORT: inPlayer    EXPORT: same
public boolean equals()
{
    boolean same;
    same = false;
    if (name.equals(PlayerClass.getName))
    {
        same = true;
    }
    return same;
}
}
  • 3
    Forgetting the paranthesis. Should be `getName()` and so on – sam Oct 31 '15 at 03:02
  • Thank you Sam! For future reference: The code then gets errors like PlayerClass.java:51: non-static method getLevel() cannot be referenced from a static context PlayerClass.setLevel(PlayerClass.getLevel() + 1); ^ Which I fixed by making the methods static. After that though, I get errors stating that I'm missing a return statement for the setLevel and levelUp mutators. But i was taught that mutators aren't supposed to return anything... – Jeremy Bunag Oct 31 '15 at 03:16
  • Usually setters don't return anything. I recommend reading answers at [Is it bad practice to make a setter return “this”?](http://stackoverflow.com/questions/1345001/is-it-bad-practice-to-make-a-setter-return-this) – sam Oct 31 '15 at 16:14

2 Answers2

0

U have missed a parenthesis on getLevel() and getName() and have in mind that you have static fields. Also, you have put

//setLevel  IMPORT: inLevel
public int setLevel(int inLevel)
 {
PlayerClass.level = inLevel;
 }

Where it should be void instead of an int if you want a setter, as well as levelUp() method.

Sekula1991
  • 288
  • 4
  • 17
0

to call a method getName on object player you would do player.getName(). You are missing parenthesis.

Semantically there are bunch of issues in your program.

  1. Why are your fields static? They seem like instance variables. (Read the difference between static fields and instance fields)

  2. Its not java convention to suffix class name with Class. In your case class name would simply be Person instead of PersonClass.

  3. Your equals method is prone to throw NullpointerException

  4. Two of your constructors have same code except for one statement. You should be able to factor out those using constructor chaining. (Read up about it)

I would write your code (using eclipse) as the following:

public class Player {
private int level, health, kills, damage;
private String name;

// Alternate Constructor: IMPORT: inName
public Player(String name, int level, int health, int kills) {
    this.name = name;
    this.level = level;
    this.health = health;
    this.kills = kills;
}

public Player() {
    this("Jeremy", 1, 100, 0);
}

public Player(Player player) {
    this.name = player.getName();
}

public int getLevel() {
    return level;
}

public void setLevel(int level) {
    this.level = level;
}

public int levelUP() {
    this.setLevel(this.getLevel() + 1);
    return this.level;
}

public int getHealth() {
    return health;
}

public void setHealth(int health) {
    this.health = health;
}

public int getKills() {
    return kills;
}

public void setKills(int kills) {
    this.kills = kills;
}

public int getDamage() {
    return damage;
}

public void setDamage(int damage) {
    this.damage = damage;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

// toString IMPORT: None EXPORT: playerStr
public String toString() {
    String playerStr;
    playerStr = "Name: " + this.name + "Level: " + this.level + "Kills: "
            + this.kills;
    return playerStr;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Player other = (Player) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

}

r3doc
  • 69
  • 6