Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Cannot find symbol

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Cannot find symbol

    hi,

    I i'm writing some code code for a school assignment, but I get stuck at a cannot find symbol error:

    javac *.java
    Ecosysteem.java:34: cannot find symbol
    symbol  : variable isKonijn
    location: interface IDier
                                            Wereld[randomRij][randomKolom].isKonijn
    = true;
                                                                          ^
    Ecosysteem.java:53: cannot find symbol
    symbol  : variable isVos
    location: interface IDier
                                            Wereld[randomRij][randomKolom].isVos = t
    rue;
                                                                          ^
    Ecosysteem.java:86: cannot find symbol
    symbol  : variable isKonijn
    location: interface IDier
                                    if (Wereld[rij][kolom].isKonijn) {
                                                          ^
    Ecosysteem.java:88: cannot find symbol
    symbol  : variable isVos
    location: interface IDier
                                    } else if (Wereld[rij][kolom].isVos) {
                                                                 ^
    4 errors

    The assignment is about writing an ecosystem with konijnen (rabbits) and vossen (foxes). The files I've written so far:

    Vos.java:
    [Java] Vos.java - Pastebin.com
    Konijn.java:
    [Java] Konijn.java - Pastebin.com
    Dier.java:
    [Java] // abstracte klasse Dier welke de interface IDier.java implementeert // // // - Pastebin.com
    IDier.java:
    [Java] IDier.java - Pastebin.com
    Ecosysteem.java
    [Java] Ecosysteem.java - Pastebin.com
    IEcosysteem.java
    [Java] IEcosysteem.java - Pastebin.com

    Can anybody explain to me why I get this error and how I can solve it?

    Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    cannot find symbol
    The error from the compiler says that the compile can NOT find a defintion for a symbol you have coded in the program. Look at the text of the error message, find the symbol that the compiler is talking about then check your code to see why that symbol is not defined in it.

    Some common errors:
    The name could be misspelled,
    or it could be out of scope. Ie not defined within the enclosing {}s

    Where are isKonijn and isVos defined?

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    isKonijn and isVos are defined in Konijn.java and Vos.java:

    The beginning of Konijn.java:
    class Konijn extends Dier {
     
            boolean isKonijn;
     
            /**
             * Deze methode returnt true als dit object een Konijn is, en anders returnt deze methode false;
             */
             public boolean isKonijn() {
                    return true;
             }

    The beginning of Vos.java:
    class Vos extends Dier {
     
            boolean isVos;
            static int gegeten;
            static int[] eet;
     
            /**
             * Deze methode returnt true als dit object een Vos is, en anders returnt deze methode false
             */
            public boolean isVos() {
                    return true;
            }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    Why can't the compiler see those definitions when it is compiling the Ecosysteem.java file?
    Are any of the class's in packages?

    Some questions about the code:
    Why are there method names with the same names as variables?
    Why do the methods return true instead of the value of some variable?

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    Quote Originally Posted by Norm View Post
    Why can't the compiler see those definitions when it is compiling the Ecosysteem.java file?
    Are any of the class's in packages?
    As far as I know, none of the class's are in a package.

    Quote Originally Posted by Norm View Post
    Some questions about the code:
    Why are there method names with the same names as variables?
    Why do the methods return true instead of the value of some variable?
    The assignment gives the IDier.java and IEcosystem.java files (can't make any changes to them) and the other files must be created by the student. The methods must be included in Konijn.java and Vos.java (only the method 'loop' must be included in Dier.java).

    Above the methods public boolean isKonijn() and public boolean isVos() stands:
    / **
    * This method returns true if this object is a Rabbit, and otherwise false;
    * /

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    Seems redundant for a method to be hardcoded to return true. Why would you call a method if you know it will always return true?

    What data type is Wereld?
    If it is a class, does that class have definitions for the two variables: (isKonijn and isVos) that the compiler can not find definitions for?

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    File structue:
    Idier.java - This is the interface for the animals
    Dier.java - implements the IDier interface (dier = animal)
    Konijn.java - extends Dier.java (konijn = rabbit)
    Vos.java - extends Dier.java (vos = fox)

    IEcosysteem.java -This is the interface to create the world
    Ecosysteem.java - implements IEcosysteem and is used to create the world. The world is a IDier[][].

    Wereld is a IDier[][]

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    Does IDier have definitions for the variables that are not found?

    You can only reference variables defined in IDier using the Wereld variable.

    Is there a confusion between referencing a variable and calling a method? You have them with the SAME names.

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    Quote Originally Posted by Norm View Post
    Does IDier have definitions for the variables that are not found?

    You can only reference variables defined in IDier using the Wereld variable.

    Is there a confusion between referencing a variable and calling a method? You have them with the SAME names.
    This was exactly what was going on, thanks for helping.

    I've done a lot of work on it and now I'm getting my next error:
    Exception in thread "main" java.lang.NullPointerException
            at Ecosysteem.run(Ecosysteem.java:88)
            at Ecosysteem.main(Ecosysteem.java:190)

    Line 190 is: w.run();
    	public static void main(String[] args) {
    		Ecosysteem w = new Ecosysteem();
       		w.Ecosysteem(3,3,1,0);
    		w.afdrukken();
     
    		for (int i = 0; i < 2; i++) {
    			w.run();
    			w.afdrukken();
    		}
    	}

    Line 88 is: if (Wereld[rij][kolom].isVos()) {
    	public void run() {
     
    		IDier[][] omgeving = {{},{}};
    		Vos vos = new Vos();
    		Konijn konijn = new Konijn();
     
    		// vossen kunnen konijnen opeten
    		for (int rij=0; rij<nrrijen; rij++) {
    			for (int kolom=0; kolom<nrkolommen; kolom++) {
    				// is deze cel een vos?
    				if (Wereld[rij][kolom].isVos()) {
     
    ... more code

    The run method needs to change the Ecosysteem w, but I didn't got it working...

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    Does Wereld[rij][kolom] have a null value?

  11. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    Quote Originally Posted by Norm View Post
    Does Wereld[rij][kolom] have a null value?
    It could. I created a field (nrrijen by nrkolommen) were a number of rabbits (konijn) and foxes (vos) are randomly placed, so some cels stay empty.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    Then you must test if null before using it.

  13. The Following User Says Thank You to Norm For This Useful Post:

    waarten (January 11th, 2012)

  14. #13
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    Fixed it with adding Wereld[rij][kolom] != null to the if statement.

  15. #14
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    Get another error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
            at Ecosysteem.run(Ecosysteem.java:169)
            at Ecosysteem.main(Ecosysteem.java:273)

    about line [I]omgeving[j] = Wereld[rijB][kolomB];
    public void run() {
     
    		IDier[][] omgeving = {{}};
    		Vos vos = new Vos();
    		Konijn konijn = new Konijn();
     
    		// vossen kunnen konijnen opeten
    		for (int rij=0; rij<nrrijen; rij++) {
    			for (int kolom=0; kolom<nrkolommen; kolom++) {
    				if (Wereld[rij][kolom] != null) {
    					// is deze cel een vos?
    					if (Wereld[rij][kolom].isVos()) {
    						// vierkant Dier-grid (3x3), met het dier zelf in het midden aanmaken
    						for (int i = 0; i < 3; i++) {
    							for (int j = 0; j < 3; j++) {
    								int rijA = -1 + i;
    								int kolomA = -1 + j;
    								int rijB;
    								int kolomB;
     
    								// onder is boven, boven is onder, rechts is links en links is rechts
    								// ofwel raster is een bol
     
    								// als rij + rijA = -1 dan nrrijen
    								if (rij + rijA == -1) {
    									rijB = nrrijen;
    								// als rij + rijA = nrrijen +1 dan 0
    								} else if (rij + rijA == nrrijen + 1) {
    									rijB = 0;
    								// anders rij + rijA
    								} else {
    									rijB = rij + rijA;
    								}
    								// als kolom + kolomA = -1 dan nrkolommen
    								if (kolom + kolomA == -1) {
    									kolomB = nrkolommen;
    								// als kolom + kolomA = nrkolommen +1 dan 0
    								} else if (kolom + kolomA == nrkolommen + 1) {
    									kolomB = 0;
    								// anders kolom + kolomA
    								} else {
    									kolomB = kolom + kolomA;
    								}
     
    								omgeving[i][j] = Wereld[rijB][kolomB];
    							}
    						}
    						// eet, eet, eet
    						vos.eet(omgeving,pvek);
    					}
    				}
    			}
    		}

    As far as i know i've taken care of illegal indexes for the array, or am i wrong?

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    What is the size of the array being used on line 169? What is the value of the index used there?

  17. #16
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    Quote Originally Posted by Norm View Post
    What is the size of the array being used on line 169? What is the value of the index used there?
    omgeving[i][j] where i and j are 0, 1 or 2 (so size 3?)
    it's empty till the cel's of Wereld[][] are linked to it.

    Wereld[][] is has the size of nrrijen (number of rows) and nrkolommen (number of columns)

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    Print out the .length of the array to see its size.
    You get an ArrayIndexOutOfBoundsException because your index is too big.
    You need to know the size of an array to be able to use it.

    it's empty
    Where do you give it a value? for example with the new statement

  19. #18
    Junior Member
    Join Date
    Jan 2012
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol

    Quote Originally Posted by Norm View Post
    Print out the .length of the array to see its size.
    You get an ArrayIndexOutOfBoundsException because your index is too big.
    You need to know the size of an array to be able to use it.


    Where do you give it a value? for example with the new statement
    omgeving has a size of 3, Wereld has a size of 10

    why cant i copy the 9 cels of Wereld to omgeving? 3 by 3 is 9 cels, right?
    Last edited by waarten; January 11th, 2012 at 01:56 PM.

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,434
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: Cannot find symbol

    Add a println just before the statement where the exception occurs and print out the values of all of the indexes being used. That will show you what the code is doing.

    What statement gives a size to omgeving? Can you post it here?

Similar Threads

  1. Cannot find symbol
    By BadgerWatch in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 6th, 2011, 10:25 PM
  2. Cannot find Symbol?
    By defmetalhead in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 5th, 2011, 08:48 AM
  3. Cannot find symbol error
    By AnuR in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 23rd, 2011, 01:50 PM
  4. Cannot find symbol?
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 10th, 2010, 09:02 PM
  5. cannot find symbol Error
    By bananasplitkids in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 9th, 2010, 01:36 AM