I keep getting "error: cannot find symbol" when trying to do constructor chaining

606 views Asked by At

So this is the part of my code that contains the constructors. As you can see, the purpose is to have the constructors take in less parameters and call the most specific constructors. I thought I initiated my values in the first constructor. Why can it not find my symbol?

Here my code:

 public class Frog{

// instance variables 
 private String name;
 private int  age;
 private double tongueSpeed;
 private boolean isFrogLet;
 private  String species;

**// Third constructor** 
public Frog( String Name){
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
 
 }


**//second constructor** 
 public Frog(String Name, int ageInYears, double TongueSpeed){
   this(Name, Age, TongueSpeed, IsFrogLet, Species);
   name= Name;
   age = ageInYears;
   tongueSpeed= TongueSpeed;
 }

**// most specific constructor**
 public Frog( String Name, int age, double TongueSpeed, boolean IsFrogLet, String Species){
   name = Name;
   this.age = Age;
   tongueSpeed = TongueSpeed;
   isFrogLet= IsFrogLet;
   species = Species;
 }

public void grow(int months){
 age = age + months;
 while ( age < 12){
  tongueSpeed++;
 }
 if (age>5 & age>30){
  double highRes= age-30;
  tongueSpeed= tongueSpeed-highRes;
 }
 if (age>1 & age <7){
  isFrogLet = true;
 }
}

}

-This is my error i'm getting:

Frog.java:12: error: cannot find symbol
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
            ^
  symbol:   variable Age
  location: class Frog
Frog.java:12: error: cannot find symbol
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
                 ^
  symbol:   variable TongueSpeed
  location: class Frog
Frog.java:12: error: cannot find symbol
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
                              ^
  symbol:   variable IsFrogLet
  location: class Frog
Frog.java:12: error: cannot find symbol
 this(Name, Age, TongueSpeed, IsFrogLet, Species);
                                         ^
  symbol:   variable Species
  location: class Frog
Frog.java:19: error: cannot find symbol
   this(Name, Age, TongueSpeed, IsFrogLet, Species);
              ^
  symbol:   variable Age
  location: class Frog
Frog.java:19: error: cannot find symbol
   this(Name, Age, TongueSpeed, IsFrogLet, Species);
                                ^
  symbol:   variable IsFrogLet
  location: class Frog
Frog.java:19: error: cannot find symbol
   this(Name, Age, TongueSpeed, IsFrogLet, Species);
                                           ^
  symbol:   variable Species
  location: class Frog
Frog.java:28: error: cannot find symbol
   this.age = Age;
              ^
  symbol:   variable Age
  location: class Frog
3

There are 3 answers

3
Tarmo On

I think you are getting the concept of constructor overloading wrong. **// Third constructor** is taking in only name, but you are still trying to pass other stuff to this. You can't pass stuff if it does not exist. So pass what comes from the parameters and set other stuff to null like so:

public Frog( String Name){
   this(Name, 0, 0.0, false, null));
}

Same applies to other constructors. See what the parameters of concrete constructors is and set other stuff to null.

0
S1LENT WARRIOR On

the error is self-explanatory. You are not creating variables with the specific names in your second and third constructor before passing them to the most specific constructor.
You should pass either pass the exact variable that you receive in your constructor to the underlying constructor or you should pass some default values to the most specific constructor instead of these variables (aka symbols).
so you should call your second and third constructors in the following ways:

// Third constructor
public Frog(String Name) {
    this(Name, 0, 0.0, false, "");
}


//second constructor
public Frog(String Name, int ageInYears, double TongueSpeed) {
    this(Name, ageInYears, TongueSpeed, false, "");
}  

Hope this helps

0
Nitesh Sharma On

Error Cause

You are using the non-existing variables as your instance-level are in camelCase no in title Case variables other than this please find refactored code and try to use camelCase for the variable

public class Frog {
    private String name;
    private int age;
    private double tongueSpeed;
    private boolean isFrogLet;
    private String species;

    public Frog(String name, int age, double tongueSpeed, boolean isFrogLet, String species) {
        this.name = name;
        this.age = age;
        this.tongueSpeed = tongueSpeed;
        this.isFrogLet = isFrogLet;
        this.species = species;
    }

    public Frog(String name) {
        this.name = name;
    }

    public Frog(String name, int ageInYears, double TongueSpeed) {
        this.name = name;
        this.age = ageInYears;
        this.tongueSpeed = TongueSpeed;
    }

    public void grow(int months) {
        age = age + months;
        while (age < 12) {
            tongueSpeed++;
        }
        if (age > 5 & age > 30) {
            double highRes = age - 30;
            tongueSpeed = tongueSpeed - highRes;
        }
        if (age > 1 & age < 7) {
            isFrogLet = true;
        }
    }

}