Hi I was looking help with a null pointer error I get everytime I use my newgame button or start the newgame method. What the game should do is guess 1-1000 then change the screen when the user's guess is too low , too high, and correct. Once the user decides to stop playing the game, i have to list statistics in a dialog box before the program ends. The statistics have to include rounds played, how many guesses they took per round and the time it took to get it right. I'm also having trouble with getting the statistic page. I would want round label on the left and at the top should have a header with guesses and time it took; In the middle should be all the user info. I dont know if an array is best to do this. Thank you in advance
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
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;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.lang.NumberFormatException;
import javax.swing.JTextArea;
public class NumbersGame extends JFrame
{
private int number;
private long t=System.currentTimeMillis();
private long tendofround;
private long r;
private int tries = 1;
private int s;
private int f=0;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextArea outText;
private JButton button1;
private JTextField textField;
private JLabel random1;
private String rounds[];
public NumbersGame()
{
super("Guess My Number");
setLayout(new FlowLayout() );
getContentPane().setBackground(Color.lightGray);
label1= new JLabel("Try to guess the number. The number can range from 1 to 1000."
+" The Background will change blue for too low and red for too high.");
add(label1);
long t=System.currentTimeMillis();
label2= new JLabel("Guesses");
add(label2);
textField = new JTextField(4);
textField.addActionListener(new GuessHandler());
add(textField);
label3=new JLabel("");
add(label3);
button1 = new JButton( "New Round" );
add(button1);
Random generator = new Random();
number = generator.nextInt(1001);
outText= new JTextArea();
button1.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
newGame();
}
}
);
}
public void newGame()
{
getContentPane().setBackground(Color.lightGray);
textField.setText("");
label3.setText("");
textField.setEditable(true);
Random generator = new Random();
number = generator.nextInt(1001);
tendofround= System.currentTimeMillis();
Time();
rounds[f]=""+tries+s;
f++;
tries=0;
t=System.currentTimeMillis();
}
public void endGame()
{
JOptionPane.showMessageDialog(null,outText);
}
public void Time()
{
r= tendofround-t;
s= (int)r/1000;
}
class GuessHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
int i= Integer.parseInt(textField.getText());
if(i<number)
{
label3.setText("Guess number is Too Low");
SwingUtilities.updateComponentTreeUI(label3);
getContentPane().setBackground(Color.BLUE);
textField.setText("");
}
else
{
label3.setText("Guess number is Too High");
SwingUtilities.updateComponentTreeUI(label3);
getContentPane().setBackground(Color.RED);
textField.setText("");
}
if(i==number)
{
label3.setText("Correct");
SwingUtilities.updateComponentTreeUI(label3);
getContentPane().setBackground(Color.GREEN);
textField.setEditable(false);
int n=JOptionPane.showConfirmDialog(null,"Do you want to play again?",null,JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
newGame();
}
else if(n==JOptionPane.NO_OPTION)
{
endGame();
}
}
tries++;
}
catch(NumberFormatException numberformatexception){
JOptionPane.showMessageDialog(null,"Please enter an integer.\nDo not use any special characters.",null,JOptionPane.ERROR_MESSAGE);
textField.setText("");
}
}
}
}
import javax.swing.JFrame;
public class NumbersGameTest
{
public static void main( String args[] )
{
NumbersGame numbersGame = new NumbersGame();
numbersGame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
numbersGame.setSize( 800, 150 );
numbersGame.setVisible( true );
}
}