0

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 (i.e. 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!

1
  • 1
    You should have a stack trace - look at that to pinpoint what's going on. You should also start using normal Java naming conventions (classes should be in PascalCase), and compare strings using equals() instead of ==. Commented Apr 8, 2011 at 11:14

3 Answers 3

1

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);
    }
    ...
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah I see. I thought that it looked sketchy, but I'm largely reusing code (permissively, of course) so it can get messed up rather easily. Thank you very much!
1

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

Comments

0

In init(), you make 2 local buttons that hide the outer ones, therefore, they are still null after init().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.