Java class “cannot be resolved to a type”

前端 未结 4 1506
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 18:50

This is the error I\'m getting:

Exception in thread \"main\" java.lang.Error: Unresolved compilation problem:  
             


        
相关标签:
4条回答
  • 2020-12-10 19:29

    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).

    0 讨论(0)
  • 2020-12-10 19:30

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

    0 讨论(0)
  • 2020-12-10 19:55

    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.

    0 讨论(0)
  • 2020-12-10 19:56

    That error can appear also if you create the inner class by moving it from somewhere, and with reorganization, you put the import line for that inner class in its outer class file. After that the inner class cannot be resolved to a type anywhere. Automatic insert of import won't help.

    You have not published the import section of the outer class and we cannot check that variant. Check for it, and if the excessive import is there, remove it.

    And if you work in Eclipse, it is very probable you need to clean the project after that.

    0 讨论(0)
提交回复
热议问题