【问题标题】:Java Applet NullPointerExceptionJava 小程序 NullPointerException
【发布时间】:2011-04-08 19:17:12
【问题描述】:

我正在开发一个 Applet,它有一个 JButton,我想用它来启用另一个 JButton。但是,当我按下按钮时,出现错误:线程“AWT-EventQueue-1”java.lang.NullPointerException 中的异常。为什么会这样?似乎当我运行 Applet 时,全局变量没有被实例化(即它们都是“null”)。在另一个程序中,一切正常,我在执行此操作方面找不到两者之间的任何区别。

这是我的一些代码:

public class implementation2 extends Applet implements ActionListener {
  private static final long serialVersionUID = -4370650602318597069L;
  ...
  public JButton coreButton, testButton;
  ...
  public void init() {
    ...
    final JButton testButton = new JButton("Test);
    testButton.addActionListener(this);
    ...
    final JButton coreButton = new JButton("CORE");
    coreButton.addActionListener(this);
    coreButton.setEnabled(false);
    ...
  }
  ...
  public void actionPerformed(final ActionEvent event) {

  if(event.getActionCommand() == "Test") {
    coreButton.setEnabled(false);
  }
  ...

如果有人能指出我修复我的代码的方向,那将不胜感激!谢谢!

【问题讨论】:

  • 你应该有一个堆栈跟踪 - 查看它以查明发生了什么。您还应该开始使用普通的 Java 命名约定(类应该使用 PascalCase),并使用 equals() 而不是 == 比较字符串。

标签: java applet jbutton


【解决方案1】:

这就是问题所在:

public JButton coreButton, testButton;

public void init() {
  final JButton testButton = new JButton("Test);

在这里,您创建了一个 local 变量,它为testButtoncoreButton 也一样)隐藏了 instance 变量。这意味着实例变量仍然为空 - 因此当您稍后尝试取消引用它们时,您会遇到异常。您不想在 init 中声明新的局部变量 - 您只想将值分配给实例变量。更正代码:

public class Implementation2 extends Applet implements ActionListener {
  private static final long serialVersionUID = -4370650602318597069L;
  ...
  public JButton coreButton, testButton;
  ...
  public void init() {
    ...
    testButton = new JButton("Test");
    testButton.addActionListener(this);
    ...
    coreButton = new JButton("CORE");
    coreButton.addActionListener(this);
    coreButton.setEnabled(false);
    ...
  }
  ...
  public void actionPerformed(final ActionEvent event) {
    if("Test".equals(event.getActionCommand())) {
      coreButton.setEnabled(false);
    }
    ...
  }
}

【讨论】:

  • 啊,我明白了。我认为它看起来很粗略,但我主要是在重用代码(当然是允许的),所以它很容易搞砸。非常感谢!
【解决方案2】:

当您将它们声明为全局时,为什么还要在 init() init() 中声明它们只需写:

 public void init() {
    ...
    testButton = new JButton("Test");
    testButton.addActionListener(this);
    ...
    coreButton = new JButton("CORE");
    coreButton.addActionListener(this);
    coreButton.setEnabled(false);
    ...
  }

您的代码中可能存在的错误:

public class implementation2 extends Applet implements ActionListener {
  private static final long serialVersionUID = -4370650602318597069L;
  ...
  public JButton coreButton, testButton;
  ...
  public void init() {
    ...
    final JButton testButton = new JButton("Test);  //---- Duplicate declaration which should not be done.
    //---- Forgot to write `"` to finish `Test` string
    testButton.addActionListener(this);
    ...
    final JButton coreButton = new JButton("CORE");  //---- Duplicate declaration which should not be done.
    coreButton.addActionListener(this);
    coreButton.setEnabled(false);
    ...
  }
  ...
  public void actionPerformed(final ActionEvent event) {

  if(event.getActionCommand() == "Test") { //--- use `.equals()` instead of `==`
    coreButton.setEnabled(false); //---- set it to `true` instead of `false`.
  }

【讨论】:

    【解决方案3】:

    在 init() 中,您创建了 2 个隐藏外部按钮的本地按钮,因此,在 init() 之后它们仍然为空。

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多