Results 1 to 9 of 9
- 09-15-2012, 12:40 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 71
- Rep Power
- 0
What is causing this Null Pointer Exception?
Hi, I get a runtime error when running this program:
Here is the error:Java 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); } }
Does anyone have any idea what's causing it?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)
Thanks
Astralogic
- 09-15-2012, 01:02 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: What is causing this Null Pointer Exception?
Check, if all the (member) variables you declare are initialized. ;)
- 09-15-2012, 03:08 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 71
- Rep Power
- 0
- 09-15-2012, 03:14 PM #4
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
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. :)
- 09-15-2012, 07:03 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: What is causing this Null Pointer Exception?
You forgot to initialize your 'lower' JButton.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-15-2012, 07:09 PM #6
Member
- Join Date
- Sep 2012
- Posts
- 26
- Rep Power
- 0
Re: What is causing this Null Pointer Exception?
Actually I was hoping he figures that out on his own.
- 09-16-2012, 11:04 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 71
- Rep Power
- 0
- 09-16-2012, 11:22 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: What is causing this Null Pointer Exception?
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-16-2012, 01:49 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 71
- Rep Power
- 0
Similar Threads
-
Null pointer exception
By jessie in forum New To JavaReplies: 5Last Post: 02-08-2011, 02:58 PM -
null pointer exception
By marvelk in forum Advanced JavaReplies: 8Last Post: 02-01-2011, 09:02 AM -
Null pointer exception
By samuel.roshni in forum Java ServletReplies: 14Last Post: 01-22-2011, 02:25 PM -
External entity in XML causing null pointer exception during DocumentBuilder.parse
By sri_kumar_4u in forum XMLReplies: 0Last Post: 12-04-2009, 12:39 PM -
Null Pointer Exception
By demiser55 in forum New To JavaReplies: 1Last Post: 09-22-2008, 06:33 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks