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.