newGameJButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { guessInputJTextField.setText(""); Random generator = new Random(); number = generator.nextInt(1001); random1.setText(""+ number); SwingUtilities.updateComponentTreeUI(random1); messageJLabel.setText(""); guessInputJTextField.setEditable(true); Guesses =0; message2JLabel.setText("NUMBER OF GUESSES: "+ Guesses); Guesses++; } } ); theGame(); } public void theGame() { } class GuessHandler implements ActionListener { public void actionPerformed(ActionEvent e) { int Guess; Guess = Integer.parseInt(guessInputJTextField.getText()); if (Math.abs(number - Guess) < Math.abs(number - GuessOld)) { getContentPane().setBackground(Color.RED); } else { getContentPane().setBackground(Color.BLUE); } GuessOld = Guess; if (Guess > number) { messageJLabel.setText("TOO HIGH! "); SwingUtilities.updateComponentTreeUI(messageJLabel); } if (Guess < number) { messageJLabel.setText("TOO LOW! "); SwingUtilities.updateComponentTreeUI(messageJLabel); } if (Guess == number) { messageJLabel.setText("CORRECT! "); getContentPane().setBackground(Color.YELLOW); SwingUtilities.updateComponentTreeUI(messageJLabel); guessInputJTextField.setEditable(false); } message2JLabel.setText("NUMBER OF GUESSES: " + Guesses++); } } }   GuessGame.java import javax.swing.JFrame; public class GuessGame { public static void main(String args[]) throws Exception { Gameframe guessgame = new Gameframe(); guessgame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); guessgame.setSize(550, 150); guessgame.setVisible(true); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Need Help Java Code Error

SEE IMAGE FOR ERROR

JAVA CODES:

Gameframe.java

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingUtilities;

public class Gameframe extends JFrame{
private int Guesses = 1;
private int GuessOld =0;
private int number;
private JTextField guessInputJTextField;
private JLabel prompt1JLabel;
private JLabel prompt2JLabel;
private JLabel messageJLabel;
private JLabel message2JLabel;
private JLabel random1 = new JLabel("");
private JButton newGameJButton;
private Color background;

public Gameframe()
{
super("Guessing Game");
setLayout(new FlowLayout());
background = Color.LIGHT_GRAY;
prompt1JLabel = new JLabel("\nI HAVE A NUMBER BETWEEN 1 AND 100.");
add(prompt1JLabel);
prompt1JLabel = new JLabel("\nCAN YOU GUESS MY NUMBER? ENTER YOUR GUESS:");
add(prompt2JLabel);
guessInputJTextField = new JTextField(5);
guessInputJTextField.addActionListener(new GuessHandler());
add(guessInputJTextField);
messageJLabel = new JLabel("");
add(messageJLabel);
message2JLabel = new JLabel("");
add(message2JLabel);
newGameJButton = new JButton("NEWGAME");
add(newGameJButton);

Random generator = new Random();
number = generator.nextInt(1001);

newGameJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
guessInputJTextField.setText("");
Random generator = new Random();
number = generator.nextInt(1001);
random1.setText(""+ number);
SwingUtilities.updateComponentTreeUI(random1);
messageJLabel.setText("");
guessInputJTextField.setEditable(true);
Guesses =0;
message2JLabel.setText("NUMBER OF GUESSES: "+ Guesses);
Guesses++;
}
}
);
theGame();
}
public void theGame()
{
}
class GuessHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int Guess;
Guess = Integer.parseInt(guessInputJTextField.getText());
if (Math.abs(number - Guess) < Math.abs(number - GuessOld))
{
getContentPane().setBackground(Color.RED);
}
else
{
getContentPane().setBackground(Color.BLUE);
}
GuessOld = Guess;
if (Guess > number)
{
messageJLabel.setText("TOO HIGH! ");
SwingUtilities.updateComponentTreeUI(messageJLabel);
}
if (Guess < number)
{
messageJLabel.setText("TOO LOW! ");
SwingUtilities.updateComponentTreeUI(messageJLabel);
}
if (Guess == number)
{
messageJLabel.setText("CORRECT! ");
getContentPane().setBackground(Color.YELLOW);
SwingUtilities.updateComponentTreeUI(messageJLabel);
guessInputJTextField.setEditable(false);
}
message2JLabel.setText("NUMBER OF GUESSES: " + Guesses++);
}
}
}

 

GuessGame.java

import javax.swing.JFrame;
public class GuessGame
{
public static void main(String args[]) throws Exception
{
Gameframe guessgame = new Gameframe();
guessgame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guessgame.setSize(550, 150);
guessgame.setVisible(true);
}
}

 

ERROR
CODE
Console: connected (Running: 222 seg)
std output
Exception in thread "main" java.lang.NullPointerException
at java.desktop/java. aut.Container.addImpl(Container.java:1117)
at java.desktop/java. aut.Container.add(Container.java:1029)
at java.desktop/javax.swing.JFrame.addImpl(JFrame.java:553)
at java.desktop/java. aut.Container.add(Container.java:436)
at Gameframe.<init>(Gameframe.java:33)
at GuessGame.main(GuessGame.java:6)
(END)
Transcribed Image Text:ERROR CODE Console: connected (Running: 222 seg) std output Exception in thread "main" java.lang.NullPointerException at java.desktop/java. aut.Container.addImpl(Container.java:1117) at java.desktop/java. aut.Container.add(Container.java:1029) at java.desktop/javax.swing.JFrame.addImpl(JFrame.java:553) at java.desktop/java. aut.Container.add(Container.java:436) at Gameframe.<init>(Gameframe.java:33) at GuessGame.main(GuessGame.java:6) (END)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY