JButton.actionPerformed: null pointer exception

后端 未结 5 653
抹茶落季
抹茶落季 2020-12-21 22:47

I\'m working through a book, and the following code throws a NPE at runtime when the JButton is clicked, at the line button.actionPerformed. I\'ve done my best to be sure m

5条回答
  •  星月不相逢
    2020-12-21 23:16

    In a method, you have this:

    JButton button = new JButton("click here");
    

    This create the variable, but the scope of this new variable is inside the method. You've already declared button in your class though. It should just be:

    button = new JButton("click here");
    

提交回复
热议问题