What causes “Can't find Symbol” and how to fix it?

前端 未结 5 1553
春和景丽
春和景丽 2020-12-19 08:06

I\'ve been trying to figure this out, I\'ve run it in different programs so it\'s definitely in the code. Probably something easy too. The error says

相关标签:
5条回答
  • 2020-12-19 08:10

    It can't find the variable password, which, as you've coded it, only exists in the Password2 constructor. You'll need to either make password a class member variable or pass it to the constructor of your Handler classes, so they can have a reference to it.

    0 讨论(0)
  • 2020-12-19 08:11
    password 
    

    is a local variable declared in the constructor of Password2. It is not in scope in your EnterButtonHandler.actionPerformed method. Make it an instance variable to resolve.

    0 讨论(0)
  • 2020-12-19 08:19

    Read the error message, love the error message.

    It takes some practice, but after awhile it's easy to see it more clearly: just read across the bold text below as a sentence :)

    error: cannot find symbol [...]

    symbol: variable password

    location: [in] class Password2.EnterButtonHandler

    There is nothing named password in that scope/context (EnterButtonHandler).

    Happy coding.


    Hint: there is a local variable with the same name in a different scope/context... perhaps it shouldn't be a local variable? See The Java Tutorial: Variables for more :)

    0 讨论(0)
  • 2020-12-19 08:25

    Your class doesn't have a definition for password. Hence, the error when passing it to the equals method.

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

    password is local to the Password2 constructor.

    It should either be passed around, or an instance variable.

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