We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,390 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

GuessingNumberGame for Java/w GUI

I need some help figuring out where I have gone wrong on my program.
It is a classic classroom program, where the program creates a random number and gives high, low response untill the user enters the correct number.
I have the GUI working and I know the action to test the number is correct. When I run it the GUI comes up properly, I can enter a number, but once I hit the button I get a really long error message. The error message starts off with "Exception in thread "AWT-EventQueue-0" java.NullPointerException.....

I have pasted my code. Could someone please let me know where I have gone wrong?

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

public class GuessGameFrames extends JFrame
{
    private Random random = new Random();
    private int number = random.nextInt(100)+1; //prevent a 0;
    public JLabel welcome;
    public JLabel enter;
    public JTextField userNo;
    public JButton guess;
    public JButton giveUp;
    public JLabel hotCold;

    public GuessGameFrames()
    {
        super("GuessGameFrames");

            //create the frame
                JFrame frame = new JFrame("Guessing Game");
                frame.setVisible(true);
                frame.setSize(400,300);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                //create all the components
                JPanel panel = new JPanel(new GridBagLayout());


                JLabel welcome = new JLabel("Welcome to the number Guessing Game!");
                JLabel enter = new JLabel("\nEnter an integer between 1 and 100.");
                JTextField userNo = new JTextField(5);
                JButton guess = new JButton("Guess");
                    guess.addActionListener(new ActionGo());
                JButton giveUp = new JButton("Give Up");
                    giveUp.setVisible(false);
                    giveUp.addActionListener(new ActionGiveUp());
                JLabel hotCold = new JLabel("");
                    hotCold.setVisible(false);

                GridBagConstraints con = new GridBagConstraints();
                //add panel to the frame add other stuff to panel
                con.insets = new Insets(10,10,10,10);
                panel.add(welcome,con);
                con.gridx = 0;
                con.gridy = 1;
                panel.add(enter,con);
                con.gridx = 0;
                con.gridy = 2;
                panel.add(userNo,con);
                con.gridx = 0;
                con.gridy = 3;
                panel.add(guess,con);
                con.gridx = 0;
                con.gridy = 4;
                panel.add(giveUp, con);
                con.gridx = 1;
                con.gridy = 4;
                panel.add(hotCold,con);
                con.gridx = 0;
                con.gridy = 5;

            frame.getContentPane().add(panel, BorderLayout.NORTH);

    }

        class ActionGo implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
            int guessNum = 0;
            boolean correct = false;

             String guess = userNo.getText();

                //error check
                try
                {
                guessNum = Integer.parseInt(guess);
                if (guessNum < 0 || guessNum > 101) throw new NumberFormatException();
                }
                catch(NumberFormatException n)
                {
                    JOptionPane.showMessageDialog(null, "Please enter a valid integer between 1 and 100.", "Error", JOptionPane.INFORMATION_MESSAGE);
                }

                    if(guessNum < number)
                        {
                            hotCold.setVisible(true);
                            hotCold.setText("To low, try again");
                            giveUp.setVisible(true);
                            userNo.setText("");
                        }
                    if(guessNum > number)
                        {
                            hotCold.setVisible(true);
                            hotCold.setText("Tow high, try again");
                            giveUp.setVisible(true);
                            userNo.setText("");
                        }
                    if(guessNum == number)
                        {
                            hotCold.setVisible(true);
                            hotCold.setText("Correct!! The number was" + number);
                            correct = true;
                        }
            }//end actionPerforemd
        }//end ActionGo
    class ActionGiveUp implements ActionListener
            {
                public void actionPerformed(ActionEvent d)
                {
                enter.setText("The number was" + number +".");
                random = new Random();
                userNo.setText("");
            }//end actionPerformed
        }//end of actionGiveUp

public static void main(String [] args) throws Exception
    {
        GuessGameFrames game = new GuessGameFrames();
    }
}

Thanks for whatever help anyone can provide.

3
Contributors
3
Replies
9 Hours
Discussion Span
9 Months Ago
Last Updated
5
Views
dragonofspam
Newbie Poster
5 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

that means you are calling an instance of an object and try to perform an action on it, or use it in any way that the jvm is expecting an actual instance, but you haven actually instantiated it yet, meaning the instance still has the default value for an Object (null).
if you check your error message closely, it will tell you on which line the error occured. start investigating what instances you use there, and it'll be a lot easier to fix the problem.

also: it's bad design to allow your main method to throw an exception, you'll not be able to catch-handle it anywhere, so you won't be able to avoid your application from crashing.

stultuske
Industrious Poster
4,379 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
  1. As stuluske said the error message includes the exact line where the problem happened, so thaT''s where to look.
  2. In your constructor you declare new variables for all your fields etc. These "mask" or "hide" the declarations you made on lines 10-15. You set values correctly for these new variables, but as local variables they are discarded at the end of the constructor. In the meantime the real variables remain uninitialised, and are null when you try to use them in your actionPerformed.
JamesCherrill
... trying to help
Moderator
8,525 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

Thank you, I will check that out.

dragonofspam
Newbie Poster
5 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0656 seconds using 2.71MB