views:

29

answers:

5

Purpose of the code : Create two Buttons(button1 and button2). When User clicks button1, change the text of button2. When User clicks button2, change the text of button1.

Here's the code I'm using :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class multiAL {
JButton button1;
JButton button2;
JFrame frame;
public static void main(String[] args) {
    multiAL setterAL = new multiAL();
    setterAL.go();
}

public void go() {
    button1 =  new JButton("Click me, I'm One");
    button2 =  new JButton("Click me, I'm Two");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(BorderLayout.WEST, button1);
    frame.getContentPane().add(BorderLayout.EAST, button2);
    frame.setVisible(true);
    button1.addActionListener(new b1L());
    button2.addActionListener(new b2L());
}

class b1L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        button2.setText("What??, you clicked 1??");
    }
}

class b2L implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        button1.setText("What??, you clicked 2??");
    }
  }
}

It compiles perfectly, but when I run it I receive following error : Exception in thread "main" java.lang.NullPointerException at multiAL.go(multiAL.java:17) at multiAL.main(multiAL.java:11)

Till now, I've encountered only compile-time errors. So there are two question which I want to ask:

1) What's wrong with the code? 2) How to track down runtime errors?

+2  A: 

But where is the

frame = new JFrame();

line of code ? Since frame is null, nothing can happen, I guess ...

Riduidel
+2  A: 

frame is not initialized, so it resolves to null and you can't call methods on null objects. Like you initialized button1 and button2 you should also initialize frame.

frame = new JFrame();
klez
+3  A: 

I believe your frame object is null. It is never initialized. You can read the runtime exception. It says multiAL.java:17

This means that in line 17 you get your NullpointerException

Shervin
These mistakes would get me killed someday.But still, one question remains. From where I start counting the lines?
MoonStruckHorrors
from the top of the file
klez
@MoonStruckHorrors, try using an IDE like eclipse. eclipse shows you the line numbers. Also, portions of the stacktrace are turned into hyperlinks, so you can click the exception and jump to the line that caused it. If you are stuck using command line *nix, then use VIM and make use of the jump-to-line command. `Jump to line number n. For example, to jump to line 42, you'd type :42`
Tim Bender
A: 

Consider renaming your class to follow Java class name conventions: CamelCase (http://en.wikipedia.org/wiki/CamelCase)

And for the problem, as pointed by other users, is the frame object not being initialized.

Add the following line BEFORE the first use of the frame object:

frame = new JFrame();
frame.setSize(500,500);
Zheileman
This has nothing to do with the question. Next time use comments. Until you can't avoid using answers as comments, or your way to 50 rep will be very long ;-)
klez
Thanks, I'm new here. I'll do it next time.
Zheileman
+1  A: 

Initialize frame before first using:

frame = new JFrame();
mykhaylo