-4

I want to create an application which chooses the number to be guessed by selecting an integer at random in the range 1-1000.The application then displays the following in a label: I have a number between 1-1000.Can you guess my number?Please enter your first guess. As each guess is input,the background color should change to either red or blue.Red indicates that the user is getting "warmer" ,and blue,"colder".

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

    class GuessNumber extends JFrame{
        private JLabel topLabel;
        private JLabel colorLabel;
        private JLabel correctLabel;
        private JTextField numberText;
        private JTextField inputText;
        private JButton playBtn;
        private int num;

        GuessNumber(){
            setSize(600,300);
            setTitle("Guess the number");
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            setLocationRelativeTo(null);

            topLabel=new JLabel("I have a number between 1 and 1000.Can you guess my number?",JLabel.CENTER);
            add("North",topLabel);

            playBtn=new JButton("Play");
            inputText=new JTextField();
            inputText.setEditable(false);
            correctLabel=new JLabel();
            colorLabel=new JLabel();

        //Read a random number when play button is clicked
            playBtn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    Random r=new Random();
                    num=r.nextInt(1001);
                    numberText.setText(Integer.toString(num));
                    inputText.setEditable(true);
                }
            });

            //Show whether your guess is close to the number or not
            inputText.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    if(Integer.parseInt(inputText.getText())>num/2){
                        colorLabel.setText("Warmer");
                        colorLabel.setBackground(Color.red);
                        colorLabel.setOpaque(true);
                    }else{
                        colorLabel.setText("Colder");
                        colorLabel.setBackground(Color.blue);
                        colorLabel.setOpaque(true);
                    }
                    if(Integer.parseInt(inputText.getText())==num){
                        correctLabel.setText("Correct!");
                        correctLabel.setVisible(true);
                        inputText.setEditable(false);
                    }
                }
            });


            JPanel centerPanel=new JPanel();
            centerPanel.setLayout(new GridLayout(3,1));
            centerPanel.add(inputText);
            centerPanel.add(colorLabel);
            centerPanel.add(correctLabel);
            add(centerPanel);
            add("South",playBtn);
            setVisible(true);
        }
    }
    class Example{
        public static void main(String args[]){
            new GuessNumber();
        }
    }

The program will compile but I get an exception in run time.I can't find the error in my program.How can I fix this? Here is the error I get:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at GuessNumber$1.actionPerformed(Example.java:34)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionP
rivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

1 Answers1

0

you forgot numberText = new JTextField();. You need to initialize a reference-variable before you can use it.

ArchLinuxTux
  • 720
  • 1
  • 9
  • 25