Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By JavaAdviser

Thread: What is causing this Null Pointer Exception?

  1. #1
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default What is causing this Null Pointer Exception?

    Hi, I get a runtime error when running this program:

    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);
    	}
    
    }
    Here is the error:

    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

  2. #2
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: What is causing this Null Pointer Exception?

    Check, if all the (member) variables you declare are initialized. ;)

  3. #3
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: What is causing this Null Pointer Exception?

    Quote Originally Posted by JavaAdviser View Post
    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.

  4. #4
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default 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. :)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: What is causing this Null Pointer Exception?

    You forgot to initialize your 'lower' JButton.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    JavaAdviser is offline Member
    Join Date
    Sep 2012
    Posts
    26
    Rep Power
    0

    Default Re: What is causing this Null Pointer Exception?

    Actually I was hoping he figures that out on his own.
    DarrylBurke likes this.

  7. #7
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: What is causing this Null Pointer Exception?

    Quote Originally Posted by JavaAdviser View Post
    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.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: What is causing this Null Pointer Exception?

    Quote Originally Posted by Astralogic View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Astralogic is offline Member
    Join Date
    Mar 2012
    Posts
    71
    Rep Power
    0

    Default Re: What is causing this Null Pointer Exception?

    Quote Originally Posted by JosAH View Post
    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.

Similar Threads

  1. Null pointer exception
    By jessie in forum New To Java
    Replies: 5
    Last Post: 02-08-2011, 02:58 PM
  2. null pointer exception
    By marvelk in forum Advanced Java
    Replies: 8
    Last Post: 02-01-2011, 09:02 AM
  3. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 02:25 PM
  4. Replies: 0
    Last Post: 12-04-2009, 12:39 PM
  5. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •