I have a weird problem. My IDE says everthing about my code is alright but when I try to run it, it says an error has occured and the program doesn't run. I've read through the code again but I simply don't know what to do to get it working. I'm hoping you could help me out with this issue. Thanks in advance.
package twobuttons;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class TwoButtons extends JFrame implements ActionListener {
JButton button1;
JButton button2;
JLabel label;
public static void main(String[] args){
new TwoButtons();
}
public TwoButtons(){
this.setTitle("Dos botones");
this.setSize(400,400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
button1.setText("hola");
button1.addActionListener(this);
button1.setActionCommand("hola");
button2.setText("Te odio.");
button2.addActionListener(this);
button2.setActionCommand("Te odio.");
button1.add(this);
button2.add(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command= e.getActionCommand();
if (e.getActionCommand().equals("hola")){
JOptionPane.showMessageDialog(this.getComponent(0), "Espero que tengas un buen día");
} else {
JOptionPane.showMessageDialog(this.getComponent(0), "Odio mi vida");
}
}}
And the error I get is:
Exception in thread "main" java.lang.NullPointerException
at twobuttons.TwoButtons.<init>(TwoButtons.java:28)
at twobuttons.TwoButtons.main(TwoButtons.java:18)
C:\Users\Juan\Desktop\Informatik 12\TwoButtons\nbproject\build-impl.xml:1040: The following error occurred while executing this line:
C:\Users\Juan\Desktop\Informatik 12\TwoButtons\nbproject\build-impl.xml:805: Java returned: 1
BUILD FAILED (total time: 1 second)