What is causing this Java “Cannot find symbol” error?

穿精又带淫゛_ 提交于 2019-12-08 18:45:08

问题


I'm modifying inherited code and keep getting a weird "cannot find symbol" error which is throwing me off.

   //======= Error =========

Compiling 1 source file to /Users/Inprimus/Projects/Workspace/Soft/build/web/WEB-INF/classes
/Users/Inprimus/Projects/Workspace/Soft/WebContent/WEB-INF/classes/fr/service/CarPeer.java:49: cannot find symbol
symbol : method addCarToCompany(java.lang.Long,fr.model.company.Car)
location: class fr.dao.CompanyDAO
cmpDAO.addCarToCompany(idCompany,car);
^
1 error

Car peer:

package fr.service;    
import fr.model.company.Car;
import fr.dao.CompanyDAO;
import fr.dao.CarDao;

public class CarPeer {
    private static CarDao carDAO= new CarDao();
    private static CompanyDAO cmpDAO = new CompanyDAO();

    public static void storeCar(Long idCompany, Car car) throws UserServiceException, Exception {
        try {
            cmpDAO.addCarToCompany(idCompany,car);
            System.out.println("Car stored : "+car.toString()+" in "+idCompany);
            carDAO.storeCar(car);
        } catch(DAOException ex) {
            throw new UserServiceException(ex.getMessage(), ex);
        }
    }
}

CompanyDao:

   package fr.dao;
    import fr.model.accounting.Cost;
    import fr.model.company.Car;

    public class CompanyDAO extends GenericDAO<Company> {

    private enum ChildType {
    COST{
    public void addChildToCompany(Company company, Object child) {
    company.addCost((Cost)child);
    }
    },
    CAR{
    public void addChildToCompany(Company company, Object child) {
    company.addCar((Car)child);
    }
    };
    public abstract void addChildToCompany(Company company, Object child);
    }
private void addChildToCompany(Long idCompany, Object child, ChildType type) throws NotFoundDAOException, AlreadyExistDAOException, Exception {
        try {
            // Begin unit of work
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            Company company = (Company) session.load(Company.class, idCompany);

            type.addChildToCompany(company, child);
            session.flush();

            // End unit of work
            session.getTransaction().commit();

        } catch (ObjectNotFoundException ex) {
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
            throw new NotFoundDAOException("Identified object " + idCompany
                    + " doesn't exist in database", ex);
        } catch (ConstraintViolationException ex) {
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
            throw new AlreadyExistDAOException("The new identity already exsits in database", ex);
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
            throw new Exception(ex);
        }
    }
    public CompanyDAO() {
    super(Company.class);
    }
    public void addCarToCompany(Long idCompany, Car car) throws NotFoundDAOException, AlreadyExistDAOException, Exception {
    addChildToCompany(idCompany, car, ChildType.CAR);
    }
    }

I have triple checked but can't find anything wrong with the code thus far. I am building it in Netbeans 7.0.1.I should mention that I get this error when I build, but I can run the web app with no issues whatsoever (yet). But I am worried this may come back to bite in the behind.


I just noticed in the file tree that above the CompanyDAO classes are similarly named files bearing the format: CompanyDAO$ChildType#.class (# corresponds to a number) I'm guessing it hasn't re-compiled the class to generate the extra child Type I added. How can I effect this?


回答1:


Is CompanyDao being compiled and available on the classpath before CarPeer?




回答2:


Most likely you're using a previously compiled class file ( which didn't have the method ) in your classpath and the system is trying to use that instead of your current source code.

Otherwise, clean up your workspace, do not depend on existing compilations and try again. This has happened to me in the past.




回答3:


I keep having the same problem (though I don't know whether it's for the same reason). For me, the only thing that works (apart from ditching this "robust" IDE) is to delete the cache. On windows, it's located in %UserProfile%\.netbeans\7.0\var\cache. I suppose on *nix, it could be under ~/.netbeans/7.0/var/cache. You have to exit NetBeans first, delete the cache, then start NetBeans again.




回答4:


Clean and build your project. If that doesn't work, then restart Netbeans. Sometimes Netbeans gives weird errors and a full restart of Netbeans and/or computer just seems to fix these unexplainable issues.




回答5:


When using Netbeans 7.2+ do the following:

  • Close all tabs and exit Netbeans;
  • Remove cache directory: ~/.cache/netbeans/VERSION. Where VERSION is the Netbeans version, i.e. 7.3.1
  • Restart Netbeans and "Clean & Build"

Also see How to clear the Cache in NetBeans




回答6:


This could be the issue because the current netbeans instance that you are running is making use of the previously build up cache during its initialization. So, in order to clear the netbeans cache you can perform any of these two steps mentioned below.

APPROACH 1
1. right click on your project name in netbeans in the projects tab.
2. click on the clean and build option.
3. The cache has been cleared.

APPROACH 2
1. Click "windows key + R" and type "%UserProfile%/appdata/local/netbeans".
2. Navigate to the cache folder and then to the folder with name specifying the netbeans version.
3. Close the Netbeans and Delete all the files present in the folder.
4. Restart the netbeans and now you will be able to see the class name.

If any of these methods did not work then it might be the case that your classpath is not correct.



来源:https://stackoverflow.com/questions/8298854/what-is-causing-this-java-cannot-find-symbol-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!