Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is causing this Java "Cannot find symbol" error?

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?

like image 375
Dark Star1 Avatar asked Nov 28 '11 16:11

Dark Star1


2 Answers

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.

like image 77
OscarRyz Avatar answered Oct 05 '22 00:10

OscarRyz


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.

like image 45
Honza Avatar answered Oct 05 '22 01:10

Honza