What is causing this Null Pointer Exception?
Hi, I get a runtime error when running this program:
Code:
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
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.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class OverUnder extends JFrame {
int round = 1;
int rand1, rand2, rand3, rand4;
JLabel label1, label2, label3, label4, orLabel, winOrLose;
JButton higher, lower;
JMenuBar menubar;
JMenu file;
JMenuItem reset, exit;
public OverUnder() {
rand1 = (int)(Math.random() * 20 + 1);
Font font = new Font("Serif", Font.BOLD, 16);
setLayout(new GridLayout(3,1));
menubar = new JMenuBar();
setJMenuBar(menubar);
file = new JMenu("File");
menubar.add(file);
reset = new JMenuItem("Reset");
file.add(reset);
exit = new JMenuItem("exit");
file.add("Exit");
systemClose s = new systemClose();
exit.addActionListener(s);
restartGame r = new restartGame();
reset.addActionListener(r);
Container pane = this.getContentPane();
//top panel setup
JPanel top = new JPanel();
top.setLayout(new GridLayout(3,4));
label1 = new JLabel(""+rand1, SwingConstants.CENTER);
label1.setFont(font);
top.add(label1);
label2 = new JLabel("", SwingConstants.CENTER);
label2.setFont(font);
top.add(label2);
label3 = new JLabel("", SwingConstants.CENTER);
label3.setFont(font);
top.add(label3);
label4 = new JLabel("", SwingConstants.CENTER);
label4.setFont(font);
top.add(label4);
pane.add(top);
//middle panel setup
JPanel middle = new JPanel();
middle.setLayout(new GridLayout(1, 3));
higher = new JButton("HIGHER");
middle.add(higher);
orLabel = new JLabel("OR", SwingConstants.CENTER);
middle.add(lower);
pane.add(middle);
Event e = new Event();
higher.addActionListener(e);
lower.addActionListener(e);
// bottom panel setup
JPanel bottom = new JPanel();
bottom.setLayout(new GridLayout(1, 1));
winOrLose = new JLabel("", SwingConstants.CENTER);
winOrLose.setFont(font);
bottom.add(winOrLose);
pane.add(bottom);
}
public class Event implements ActionListener {
public void actionPerformed(ActionEvent e) {
String option = e.getActionCommand();
if (round == 1) {
rand2 = (int)(Math.random() * 20 + 1);
label2.setText("" + rand2);
if (rand2 > rand1 && option.equals("HIGHER")) {
winOrLose.setText("Right, two more!");
} else if (rand2 < rand1 && option.equals("HIGHER")) {
winOrLose.setText("You lost!");
lower.setEnabled(false);
higher.setEnabled(false);
} else if(rand2 > rand1 && option.equals("LOWER")) {
winOrLose.setText("You Lose!");
lower.setEnabled(false);
higher.setEnabled(false);
} else if (rand2 < rand1 && option.equals("LOWER")) {
winOrLose.setText("Right! Two left!");
}
round = 2;
} else if (round == 2) {
rand3 = (int)(Math.random() * 20 + 1);
label3.setText("" + rand3);
if (rand3 > rand2 && option.equals("HIGHER")) {
winOrLose.setText("Right, just one left!");
} else if (rand3 < rand2 && option.equals("HIGHER")) {
winOrLose.setText("You lost!");
lower.setEnabled(false);
higher.setEnabled(false);
} else if(rand3 > rand2 && option.equals("LOWER")) {
winOrLose.setText("You Lose!");
lower.setEnabled(false);
higher.setEnabled(false);
} else if (rand3 < rand2 && option.equals("LOWER")) {
winOrLose.setText("Right! One left!");
}
round = 3;
} else if (round == 3) {
rand4 = (int)(Math.random() * 20 + 1);
label4.setText("" + rand4);
if (rand4 > rand3 && option.equals("HIGHER")) {
winOrLose.setText("You won the game!");
} else if (rand4 < rand3 && option.equals("HIGHER")) {
winOrLose.setText("You lost!");
lower.setEnabled(false);
higher.setEnabled(false);
} else if(rand4 > rand3 && option.equals("LOWER")) {
winOrLose.setText("You Lose!");
lower.setEnabled(false);
higher.setEnabled(false);
} else if (rand4 < rand3 && option.equals("LOWER")) {
winOrLose.setText("You won the game!");
}
}
}
}
public class systemClose implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public class restartGame implements ActionListener {
public void actionPerformed(ActionEvent e) {
rand1 = (int)(Math.random() * 20 + 1);
round = 1;
higher.setEnabled(true);
lower.setEnabled(true);
label1.setText("" + rand1);
label2.setText("");
label3.setText("");
label4.setText("");
winOrLose.setText("");
}
}
public static void main(String[] args) {
OverUnder gui = new OverUnder();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(300,150);
gui.setTitle("Higher or lower");
gui.setResizable(false);
gui.setVisible(true);
}
}
Here is the error:
Quote:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at OverUnder.<init>(OverUnder.java:77)
at OverUnder.main(OverUnder.java:175)
Does anyone have any idea what's causing it?
Thanks
Astralogic
Re: What is causing this Null Pointer Exception?
Check, if all the (member) variables you declare are initialized. ;)
Re: What is causing this Null Pointer Exception?
Quote:
Originally Posted by
JavaAdviser
Check, if all the (member) variables you declare are initialized. ;)
They are all initialized as far as I can see. rand1 to 4 aren't initialized when they are declared but they are all initialized eventually with a random number.
Re: What is causing this Null Pointer Exception?
As the error message hints the problem lies within the GUI elements (awt) of the program. You have to read the error message from the bottom up. The bottommost line, where it says OverUnder.java:175, referes to the line in the main method where your class is instantiated. From there it goes up to the constructor of the class. Look what statement is at line 77 and in combination with my earlier post you should be able to spot the cause of the NullPointerException. :)
Re: What is causing this Null Pointer Exception?
You forgot to initialize your 'lower' JButton.
kind regards,
Jos
Re: What is causing this Null Pointer Exception?
Actually I was hoping he figures that out on his own.
Re: What is causing this Null Pointer Exception?
Quote:
Originally Posted by
JavaAdviser
Actually I was hoping he figures that out on his own.
Yes, that would make it less likely for me to make the same mistake again, thanks. Although I do feel like I've learned from this.
Re: What is causing this Null Pointer Exception?
Quote:
Originally Posted by
Astralogic
Yes, that would make it less likely for me to make the same mistake again, thanks. Although I do feel like I've learned from this.
NPEs are easy to spot: find the first line (from the top) in the stacktrace that refers to your code; in your case that's line #77; all the objects on that line that are null may be the cause of the exception; in your case the objects are 'middle' and 'lower' but line #75 succeeded so 'middle' can't be null, so it's 'lower' that is null. Reading a stacktrace does help.
kind regards,
Jos
Re: What is causing this Null Pointer Exception?
Quote:
Originally Posted by
JosAH
NPEs are easy to spot: find the first line (from the top) in the stacktrace that refers to your code; in your case that's line #77; all the objects on that line that are null may be the cause of the exception; in your case the objects are 'middle' and 'lower' but line #75 succeeded so 'middle' can't be null, so it's 'lower' that is null. Reading a stacktrace does help.
kind regards,
Jos
Thanks, I now feel capable of handling these kinds of errors on my own now.