Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Junky

Thread: error: cannot find symbol

  1. #1
    jaster is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default error: cannot find symbol

    Hello, I am new to Java and I can't find how to fix this problem that I am having, I hope you can :) thanks

    in: javac Grelha.java ( no errors )

    in: javac principal.java ->

    principal.java:3: error: cannot find symbol
    Grelha g = new Grelha(10,10);
    ^
    symbol: class Grelha
    location: class principal
    principal.java:3: error: cannot find symbol
    Grelha g = new Grelha(10,10);
    ^
    symbol: class Grelha
    location: class principal
    2 errors


    Code in Grelha:

    public class Grelha {
    private double espacamento;
    private int comprimento;
    private int altura;
    public int[][] mapa;
    public Grelha (int comp, int alt) {
    //this.espacamento=esp;
    this.comprimento=comp;
    this.altura=alt;
    for (int i=0;i<comprimento;i++) {
    for (int j=0;j< altura; j++) {
    mapa[i][j]=0;
    }
    }
    }
    }

    Code in principal:

    public class principal {
    public static void main(String argv[]) {
    Grelha g = new Grelha(10,10);
    for (int i=0;i<10;i++) {
    for (int j=0;j<10;j++) {
    System.out.print(g.mapa[i][j]+ " ");
    }
    System.out.print("\n");
    }
    }
    }

    Thank you very much for your help

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: error: cannot find symbol

    Where is the Grelha class? If it is in a different package then the Principal class will need to import it.

  3. #3
    srossd is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: error: cannot find symbol

    "cannot find symbol" just means that java doesn't understand your code - it's usually caused by a typo, at least in my experience. In your case, it seems like principal can't access Grelha - make sure Grelha is compiled, and both classes are in the same directory.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: error: cannot find symbol

    Quote Originally Posted by srossd View Post
    both classes are in the same directory.
    This is not good advice. There may be a very good reason why different classes are in different packages. Especially when you get to work on larger, more complex applications. Having 10,000+ classes in the same directory is a bad idea.
    Fubarable likes this.

  5. #5
    srossd is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: error: cannot find symbol

    I realize my advice doesn't apply everywhere, but with something this simple, it's probably much easier and more direct to put two classes in the same folder than to create a package and have principal import it.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: error: cannot find symbol

    But you have no idea what the context is. What if this IS an assignment on packages? The instructions might implicitly state to place the classes into different packages.

  7. #7
    srossd is offline Member
    Join Date
    Nov 2011
    Posts
    9
    Rep Power
    0

    Default Re: error: cannot find symbol

    That's certainly possible - I was just trying to give simple advice for what I consider to be the most likely scenario.

Similar Threads

  1. error: Cannot find symbol
    By iswan in forum AWT / Swing
    Replies: 3
    Last Post: 09-30-2011, 04:02 PM
  2. Cannot find symbol error?
    By Josep_16 in forum New To Java
    Replies: 2
    Last Post: 06-19-2011, 07:14 PM
  3. Cannot find symbol error
    By rajivjoshi in forum New To Java
    Replies: 3
    Last Post: 05-31-2010, 10:13 AM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. 'Cannot find symbol' error
    By minihazard10 in forum New To Java
    Replies: 6
    Last Post: 10-10-2008, 04:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •