简体   繁体   中英

Java Applet NullPointerException

I'm working on an Applet, which has a JButton that I want to use to enable another JButton. However, when I press the button, I get the error: Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException. Why is this happening? It seems as though when I run the Applet, the global variables don't get instantiated (ie they are all "null"). In another program, everything works fine, and I can't find any difference between the two in terms of implementing this action.

Here is a bit of my code:

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

If anyone can point me in the direction towards fixing my code, that would be greatly appreciated! Thank you!

This is the problem:

public JButton coreButton, testButton;

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

Here you've created a local variable which shadows the instance variable for testButton (and the same for coreButton ). That means the instance variables are still null - so when you try to dereference them later, you get an exception. You don't want to declare new local variables in init - you just want to assign the values to the instance variables. Corrected code:

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

When you are declaring them global then why again declare them in init() in init() just write:

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

Possible bugs in your code:

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`.
  }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM