Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class "cannot be resolved to a type"

This is the error I'm getting:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:  
    TeamLeader cannot be resolved to a type

    at TeamLeadDemo.main(TeamLeadDemo.java:26)

This is my code:

import java.util.Scanner;

public class Employee {
    public String empName, empNumber, hireDate;    

    public class TeamLeadDemo {}

    public Employee(String empName, String empNumber, String hireDate) {
        this.setEmpName(empName);
        this.setEmpNumber(empNumber);
        this.setHireDate(hireDate);
    }

    public void setEmpName(String empName) {
         this.empName = empName;
    }
        public void setEmpNumber(String empNumber) {
         this.empNumber = empNumber;
    }
    public void setHireDate(String hireDate) {
         this.hireDate = hireDate;
    }
    public String getEmpName() {
        return empName;
    }
    public String getEmpNumber() {
        return empNumber;
    }
    public String getHireDate() {
        return hireDate;
    }

    public class ShiftSupervisor extends Employee {
        public double annualSalary, annualProduction;
        //constructor
        public ShiftSupervisor(String empName, String empNumber,
                               String hireDate, double annualSalary,
                               double annualProduction) {
            super(empName,empNumber, hireDate);
            this.setAnnualSalary(annualSalary);
            this.setAnnualProduction(annualProduction);
        }

        public double getAnnualSalary() {
            return annualSalary;
        }
        public double getAnnualProduction() {
            return annualProduction;
        }
        public void setAnnualSalary(double annualSalary) {
            this.annualSalary = annualSalary;
        }
        public void setAnnualProduction(double annualProduction) {
            this.annualProduction = annualProduction;
        }

        public String toString() {
            return "Name: "+ getEmpName() + "\nEmpID: "+ getEmpNumber()
                   + "\nHire Date: "+ getHireDate() + "\nAnnual Salary: "
                   + annualSalary + "\nProduction: "+ annualProduction;
        }

        public class employeeStart {
            public void main(String[] args) {
                String name, id, date;
                double sal, prod;

                //create scanner object
                Scanner keyboard = new Scanner(System.in);

                //inputting data
                System.out.println("Enter Name: ");
                name = keyboard.nextLine();
                System.out.println("Enter id: ");
                id = keyboard.nextLine();
                System.out.println("Enter Hire Date: ");
                date = keyboard.nextLine();
                System.out.println("Enter Annual: ");
                sal = keyboard.nextDouble();
                System.out.println("Enter production: ");
                prod = keyboard.nextDouble();

                //instantiating object
                ShiftSupervisor pw = new ShiftSupervisor(name, id, date, sal, prod);

                //outputting data
                System.out.println("Employee Details: \n" + pw);
            }
        }
        public class TeamLeader {
            public double monthlyBonus;
            public int minTraining, trainingPresent;    

            public TeamLeader(double monthlyBonus, int minTraining, int trainingPresent) {
                this.setMonthlyBonus(monthlyBonus);
                this.setMinTraining(minTraining);
                this.addtrainingPresent(trainingPresent);                    
            }

            public void setMonthlyBonus(double monthlyBonus) {
                this.monthlyBonus = monthlyBonus;
            }
            public void setMinTraining(int minTraining) {
                this.minTraining = minTraining;
            }
            public void setTrainingPresent(int t) {
                trainingPresent = t;
            }
            public void addtrainingPresent(int hours) {
                trainingPresent += hours;
            }
            public double getMonthlyBonus() {
                return monthlyBonus;
            }
            public int getMinTraining() {
                return minTraining;
            }
            public int getTrainingPresent() {
                return trainingPresent;
            }
            public String toString() {
                return "Bonus: "+ getMonthlyBonus() + "\nMinimum Training: "
                + getMinTraining() + "\nAttendence: "+ getTrainingPresent();
            }
        }
    }
}

In addition, I declared this in a separate class:

import java.util.Scanner;

public class TeamLeadDemo extends Employee {
    public TeamLeadDemo(String empName, String empNumber, String hireDate) {
        super(empName, empNumber, hireDate);
        // TODO Auto-generated constructor stub
    }

public static void main(String[] args) {
    double sal;
    int min, atten;

    //create scanner object
    Scanner keyboard = new Scanner(System.in);

    //inputting data
    System.out.println("Enter minimum training: ");
    min = keyboard.nextInt();
    System.out.println("Enter id: ");
    atten = keyboard.nextInt();
    System.out.println("Enter Bonus: ");
    sal = keyboard.nextDouble();

    //instantiating object
    ShiftSupervisor pw = new TeamLeader(sal, min, atten);

    //outputting data
    System.out.println("Employee Details:\n" + pw);

    }
}

What is causing this error and how might I resolve it?

EDIT: Indentation, whitespace, naming conventions and readability issues have been somewhat addressed.

like image 758
lonesarah Avatar asked Feb 26 '11 04:02

lonesarah


3 Answers

If this problem is with maven project then right-click Maven > Update Project should solve the problem

like image 113
Surya Prakash Avatar answered Nov 11 '22 14:11

Surya Prakash


The problem is that TeamLeader appears to be an inner class (hard to tell, your indentation is bad), and since it's not static, you can't instantiate it by itself.

You either need to make TeamLeader its own class, or make it a static class and instantiate it as Employee.TeamLeader (or whatever the parent class is, your indentation is really not helpful here).

like image 22
EboMike Avatar answered Nov 11 '22 15:11

EboMike


Your exception message says this

Exception in thread "main" java.lang.Error: Unresolved compilation problem: TeamLeader cannot be resolved to a type

This means that you have tried to run a program that used some class that has not compiled correctly.

Go back and fix the compilation error(s).

It is not clear what the fix for the compilation error should be, but the error is saying that it cannot find a class called TeamLeader. Perhaps it doesn't exist. Perhaps it is in a different package. Perhaps you haven't compiled it. Perhaps something else.


Looking at the code, I think the problem is that you have defined two distinct classes called TeamLeadDemo, one as a nested class of Employee and the second as a top-level class. Furthermore the second of these is attempting to use TeamLeader ... but the TeamLeader class that you have actually declared is nested 2 levels down in Employee; i.e. its real name is Employee.ShiftSupervisor.TeamLeader.

This is all a bit nonsensical.

By the look of it, you defined a whole bunch of classes in the same file ("Employee.java") without much understanding of what it means to put one class inside another. Most likely, each of those classes should be declared in its own file. And most likely you should just delete the nested TeamLeadDemo class.

like image 3
Stephen C Avatar answered Nov 11 '22 16:11

Stephen C