Cannot Resolve Symbol

In JavaLanguage, if you get 'cannot resolve symbol', you have used a name that the compiler hasn't recognized.

Class names --

If it is a class name, the compiler cannot find the class. This means that the class has not been imported properly, the name is misspelt (including incorrect cAsE) or the class simply does not exist.

Another possibility, pointed out by DavidBlaikie, is that the class is not available on the command line, and a common cause of this is having your current working directory set to somewhere inside other packages, rather than at the project's root. This applies if you have either no CLASSPATH set, or a CLASSPATH which has '.' (the current directory) in it.

Variable namess --

If it is a variable name, you did not declare your variable before attempting to use it, or you misspelled it (that includes having the incorrect case). Another possibility is that the variable is in the wrong scope, e.g.:

class X {

   int x=5;
}

class Y {

   public static void main(String[] args)
   {
      System.out.println(x); //x cannot be resolved

//now for a working version: int x=6; System.out.println(x); }
}

Methods --

On a method name, the same things apply as for variable names, plus you may have given the method the wrong parameter types, e.g., calling add(int) where you should have called add(Object).

You could also get this error when trying to invoke a constructor that doesn't exist or is not visible, e.g.:

    class X
    {
private X(int y)
{
}
    }

X x=new X(5); //this won't work, remove 'private'.

X x=new X("Hello"); //this won't work, add a constructor as follows:

class X { X(int y) { }

public X(String string) { } }

X x=new X("Hello"); //this works now.
A small point with the above is that instead of just removing 'private' you should probably replace it with 'public'.

Constructors --

If you called a constructor with the wrong parameters, you can get a cannot resolve symbol error, but the error will include the word 'constructor'. Check the original constructor to see what you should have used. Check the types of parameter in the API, compared to the types that you used (as reported in the compiler error message). The order of the types should be the same too.

-- RickyClarkson


CategoryJava


EditText of this page (last edited May 12, 2012) or FindPage with title or text search