• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cannot find symbol symbol error

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a self-designed homework assignment for a Java course and may have bitten off more than I can chew. The project is a very simple contact management application that will write to and retrieve data from a mySQL database. I am getting "cannot find symbol" compile errors and cannot figure out why. I have three files for this project (in the same directory) and I think it may be a CLASSPATH issue, but that is just a guess.

We are using TextPad for the course. In TextPad I set the CLASSPATH variable to find the mySQL driver: c:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.1.24-bin.jar; .

The java files are located in a different directory from the driver.

Any pointers are most appreciated! I am on day 3 of beating my head against the wall trying to figure this out. Here are the compile errors:



The first class/file defines a Contact:


The second file/class connects to the database and defines the queries. Here is a simplified version:


The third file sets up the GUI:

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the exact command that you are firing and from which directory are you firing it? Are you sure that all the 3 source files are located in the same package? In what order are you compiling the source files?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are also short of a } in ContactQueries line 33. You can see that clearly because, unlike the Contact text, you have correctly indented ContactQueries.
 
Trish Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the help!

Campbell Ritchie: I added the missing } in line 33 of ContactQueries. I am looking and looking and looking at Contacts but I don't see the indent error you mentioned. Can you tell me which line(s)?

Mansukhdeep Thind: I am compiling within TextPad, not a command line interface. Not sure what you mean by which command I am "firing." My understanding, which is shaky, is that I need to compile from the file containing the main method. That would be ManageContacts, so I am issuing the compile command while in that file. In previous assignments, that has compiled all of the necessary files. Maybe there is another way to do it.

I did compile each file individually just to see what would happen. Contacts compiles correctly. ContactQueries issues the following error:

C:\Users\Trish\Documents\CIT 249 Java II\special project\ContactQueries.java:22: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName("com.mysql.jdbc.Driver");
^
1 error

Tool completed with exit code 1
 
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

C:\Users\Trish\Documents\CIT 249 Java II\special project\ContactQueries.java:22: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName("com.mysql.jdbc.Driver");



You have to add the mysql driver jar to the classpath.


Cheers,

 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carles Gasques wrote:

C:\Users\Trish\Documents\CIT 249 Java II\special project\ContactQueries.java:22: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName("com.mysql.jdbc.Driver");



You have to add the mysql driver jar to the classpath.


Cheers,



I don't think that is the problem Carles. She has to either declare or handle this checked exception using a try{} catch{} block. That's what the message says.

@ Trish: There is a rule in Java regarding checked exceptions that states that these type of exceptions must be either handled or declared by the method. You should read about these here. In your case, you need to say:


 
Carles Gasques
Ranch Hand
Posts: 199
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Upps,

You are right I read to quick.
Better close my mouth.
 
Trish Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:
You have not closed the try {} block correctly in the ContactQueries class.



I added the } that was missing at line 33 to close the if statement. I am just not seeing where the try block is not closed correctly. Can you give me a line number?

 
Trish Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carles Gasques wrote:You have to add the mysql driver jar to the classpath.



I thought that's what I was doing here: "We are using TextPad for the course. In TextPad I set the CLASSPATH variable to find the mySQL driver: c:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.1.24-bin.jar; . " Is this not correct?

I am sorry to be confused! It is an online course that I am taking and I am spinning my wheels much of the time. Trish
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not the problem here Trish. Read my previous reply about handle or declare rule for checked exceptions. That is the problem.
 
Trish Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mansukhdeep Thind wrote:
@ Trish: There is a rule in Java regarding checked exceptions that states that these type of exceptions must be either handled or declared by the method. You should read about these here.



I added that and it compiles! THANK YOU!!!

I have read about exceptions, but my grasp of Java as a whole is extremely tenuous. I don't understand how this error

points to not catching an exception.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java compiler has to resolve class inter dependencies Trish. that means if an object in class ManageContacts holds a reference to another class Contact, then until and unless the Contact class compiles correctly without errors, the dependent class ManageContacts will not be able to find the symbol for Contact that you have declared at line 8 as:




 
This guy is skipping without a rope. At least, that's what this tiny ad said:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic