JButton.actionPerformed: null pointer exception

后端 未结 5 652
抹茶落季
抹茶落季 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:08

    The reason is this Line:

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

    Here you are creating new local JButton object which is shadowing the member variable button . Hence button is still null. You should instead use:

    button = new JButton("click here");
    

提交回复
热议问题