简体   繁体   中英

Where does this compiling error come from?

I am getting quite frustrated right now, and the main source of my frustration is the following error:

LogicalAutomaton.java:16: error: cannot find symbol
        Cellular[i][j] = new Cellular (null, Cellular[i+1][j], null, Cellular[i][j+1]);
        ^
symbol:   variable Cellular
location: class LogicalAutomaton

The class hierarchy is the following:

home --> code --> Automata --> automata

And here is the code:

This is the class Cellular.java, in home/code/Automata/automata:

package automata;

public class Cellular {

  private int state;
  private Cellular upper, lower, left, right;

  public Cellular (Cellular upper, Cellular lower, Cellular left,Cellular right) {
    this.state = 0;
    this.upper = upper;
    this.lower = lower;
    this.left = left;
    this.right = right;
  }

}

And this is the class LogicalAutomaton.java, in home/code/Automata:

import automata.Cellular;
import java.util.Arrays;

public class LogicalAutomaton extends Cellular {

  public static void main (String[] args) {

    int n = 3;
    Cellular[][] a = new Cellular[n][n];

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == 0) {
          if (j == 0) {
            Cellular[i][j] = new Cellular (null, Cellular[i+1][j], null, Cellular[i][j+1]);
          }
          else {
            if (j == n-1) {
              Cellular[i][j] = new Cellular (null, Cellular[i+1][j], Cellular[i][j-1], null);
            }
            else {
            Cellular[i][j] = new Cellular (null, Cellular[i+1][j], Cellular[i][j-1], Cellular[i][j+1]);
            }
          }
        }
      }
    }

  }

}

The code of LogicalAutomaton.java is much longer, but the main idea is to get access to the array of Cellular objects in order to initialize them, and each time I try to do this, it throws me said error. Funnily enough, the compiler does not lament the declaration of the Cellular array.

Do you have any idea where I might have done wrong?

Thank you in advance!

Edit:

Sorry for the clumsy question; here is part of the faulty code.

You define your array as

Cellular[][] a = new Cellular[n][n];

but then later on, you use

Cellular[i][j] = new Cellular (null, Cellular[i+1][j], null, Cellular[i][j+1]);

when in fact, you should be using

a[i][j] = new Cellular (null, Cellular[i+1][j], null, Cellular[i][j+1]);

The error message you are getting

LogicalAutomaton.java:16: error: cannot find symbol Cellular[i][j] = new Cellular (null, Cellular[i+1][j], null, Cellular[i][j+1]); ^ symbol: variable Cellular location: class LogicalAutomaton

is saying I cannot find a variable named Cellular, not that I cannot find the class. This is because you have not defined a variable called Cellular , the variable you have defined is called a .

This problem is because of accessibility. The Cellular class is not accessible in the LogicalAutomoton class. Check your CLASSPATH variable and check whether the import statement is correct.

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