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()

            //create the frame
                JFrame frame = new JFrame("Guessing Game");

                //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.addActionListener(new ActionGiveUp());
                JLabel hotCold = new JLabel("");

                GridBagConstraints con = new GridBagConstraints();
                //add panel to the frame add other stuff to panel
                con.insets = new Insets(10,10,10,10);
                con.gridx = 0;
                con.gridy = 1;
                con.gridx = 0;
                con.gridy = 2;
                con.gridx = 0;
                con.gridy = 3;
                con.gridx = 0;
                con.gridy = 4;
                panel.add(giveUp, con);
                con.gridx = 1;
                con.gridy = 4;
                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
                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.setText("To low, try again");
                    if(guessNum > number)
                            hotCold.setText("Tow high, try again");
                    if(guessNum == number)
                            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();
            }//end actionPerformed
        }//end of actionGiveUp

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

Thanks for whatever help anyone can provide.

Recommended Answers

All 5 Replies

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.

  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.

Thank you, I will check that out.

how can i change my java aaplication into a servlet??
in fact i made a game and i want to make it a srvlet so that i can access it through web..

1. don't hijack old threads, especiall not if your post is not related to the original topic.
2. by rewriting it in jsp/servlet. if you have worked in layers, you'll be able to re-use the business logic classes you have, but the entire front-end 'll have to be re-written.
3. what have you done so far? we're not mindreaders, you know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.