简体   繁体   中英

Cannot find symbol when calling a method

I have two seperate java files, but both in the same folder, so calling methods should be easy. The problem is here;

    paDelimo = nota.vrniOktavo() + nota.vrniIndeks() + this.razmak * indeks;

Here it says cannot find symbol. It tells me that something is wrong with the nota.vrniOktavo() call What it does, is, it calls for a method in the other Java file. I have no idea why it doesnt work?

What should the call be like? It is calling for the method, included in this code, below. Below this code, you will also find the whole code for the file which the problematic line belongs to.

  public class Nota{
private int oktava;
private int indeks;



public Nota(int oktava, int indeks){
    this.oktava = oktava;
    this.indeks = indeks;
}

public int vrniOktavo(){
    return this.oktava;
}

public int vrniIndeks(){
    return this.indeks;
}
}

And here is the rest of the code, coming from the part that does not work;

public class Lestvica{

private int razmak;
private Nota zacetnaNota;


public Lestvica(Nota zacetnaNota, int razmak){
    this.zacetnaNota = zacetnaNota;
    this.razmak = razmak;
}
 public Nota clen(int indeks){
    int paDelimo = 0;
    int dobljenaOktava = 0;
    int dobljeniIndeks = 0;

    paDelimo = nota.vrniOktavo() + nota.vrniIndeks() + this.razmak * indeks;

    dobljenaOktava = paDelimo / 12;
    dobljeniIndeks = paDelimo % 12;
    Nota drugaNota = new Nota(dobljenaOktava, dobljeniIndeks);
    return drugaNota;
}

nota is undifined. You have an instance of Nota which is named zacetnaNota , therefore you should write:

paDelimo = zacetnaNota.vrniOktavo() + zacetnaNota.vrniIndeks() + this.razmak * indeks;

Your problem is that of an undeclared variable.

In the method

public Nota clen(int indeks){
    int paDelimo = 0;
    int dobljenaOktava = 0;
    int dobljeniIndeks = 0;

    paDelimo = nota.vrniOktavo() + nota.vrniIndeks() + this.razmak * indeks;

    // code continues
}

you are trying to assign the variable paDelimo a value from the object named nota , which is not created or available in the scope of the method. The only instance of class Nota available in this class is zacetnaNota . But you are trying to read from nota .

So, if you've confused the name of the variable, rewrite the line as

paDelimo = zacetnaNota.vrniOktavo() + zacetnaNota.vrniIndeks() + this.razmak * indeks;

Or create another instance of the Nota class, named nota , to resolve the missing reference.

Or you can Implement getter for zacetnaNota :

private Nota zacetnaNota;

public Nota getZacetnaNota() {
    return zacetnaNota;
}

Then :

 paDelimo = getZacetnaNota().vrniOktavo() + getZacetnaNota().vrniIndeks() + this.razmak * indeks;

You are wanting to call zacetnaNota, not nota, in the Lestvica class.

Here is the fixed code:

public class Lestvica {

private int razmak;
private Nota zacetnaNota;

public Lestvica(Nota zacetnaNota, int razmak) {
    this.zacetnaNota = zacetnaNota;
    this.razmak = razmak;
}

public Nota clen(int indeks) {
    int paDelimo = 0;
    int dobljenaOktava = 0;
    int dobljeniIndeks = 0;

    paDelimo = zacetnaNota.vrniOktavo() + zacetnaNota.vrniIndeks() + this.razmak * indeks;

    dobljenaOktava = paDelimo / 12;
    dobljeniIndeks = paDelimo % 12;
    Nota drugaNota = new Nota(dobljenaOktava, dobljeniIndeks);
    return drugaNota;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM