
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What Can Cause the Cannot Find Symbol Error in Java
The “cannot find symbol” error occurs mainly when we try to reference a variable that is not declared in the program which we are compiling, it means that the compiler doesn’t know the variable we are referring to.
Some possible causes for “Cannot find symbol” to occur are
- Using a variable that is not declared or outside the code.
- Using wrong cases (“tutorials” and “Tutorials" are different) or making spelling mistakes.
- The packaged class has not been referenced correctly using an import declaration.
- Using improper identifier values like letters, numbers, underscore and dollar sign. The hello-class is different from helloclass.
Example
public class CannotFindSymbolTest { public static void main(String[] args) { int n1 = 10; int n2 = 20; sum = n1 + n2; System.out.println(sum); } }
Output
CannotFindSymbolTest.java:5: error: cannot find symbol sum = n1 + n2; ^ symbol: variable sum location: class CannotFindSymbolTest CannotFindSymbolTest.java:7: error: cannot find symbol System.out.println(sum); ^ symbol: variable sum location: class CannotFindSymbolTest
In the above program, "Cannot find symbol" error will occur because “sum” is not declared. In order to solve the error, we need to define “int sum = n1+n2” before using the variable sum.
Advertisements