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 ); 
   } 
}

The String array is a good idea. It looks like you've got everything you need for the stats in the array. Are you trying to print the results in the endGame() method?
I ask because you're using outText, but I couldn't find a place where you updated outText with the information stored in the array.

In the endGame() method try adding;

String result = "";//You only need this variable long enough to use the setText method on outText
for(int i =0; i < rounds.length; i++)
{
  result = rounds[i] + "\n";
}
outText.setText(result);
JOptionPane.showMessageDialog(null, outText);

Of course you could just put result in the JOptionPane method call.
This should fix your endGame stats.

One method you may want to check out to keep up with the stats during the game is the append method for JTextArea. It may help you take the stats from a previous rounds and display them in the text area before exiting the game.
I hope this helps

Yea i wanted to use a JTextArea append within the contructor, with the name of OutText but i just couldnt' figure out how to do it . Im also still having problems with my "New Game" Button or it might be the method. I dont know how to copy the error, it says NullPointerException at numberGAme.newgame( numberGame.java:98) at actionperformed(numbersgame.java:77). I also seem to get the same exact error in my endGame stats, when i hit "no". Other than those 3 things everything seems to be running right.

Null pointer exception only occurs when you are calling any variable without defining it , or calling the function that never exists, and possibly your ActionListener are not well

I think I figured out the null pointer. It was caused by the array since it wasn't initialized. What would be the best way to print the array so it all fits in the dialogbox?

You are going to need a loop to get the information out of the array.
As far as making it fit in the dialog box, if the user only plays a few games then there shouldn't be a problem. But if the user plays a lot of games eventually there will be too much to fit into the box.

You can try adding a scroll bar to the JOption Pane.
outText is a text area, so you can add that to a scroll pane

JScrollPane scroll = new JScrollPane(outText);
//then use scroll in the JOptionPane
JOptionPane.showConfirmDialog(null, scroll, "End Game Stats", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

Putting the info. in a scroll pane is the only way I can think of that would always display stats, no matter how many games are played.

This question has already been answered. Start a new discussion instead.