Hello everyone,

I am building a tic-tac-toe game for my final and everything is going smoothly except for one problem. The names I ask the user to enter at the beginning of the game, a replaced with null when the messagebox that shows who won appears. Here is the code,

     * @(#)FrankTic.java
     *
     *
     * @author
     * @version 1.00 2012/6/15
     */

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


    public class RamiTic implements ActionListener {




    public Color fillCol;
    /*Instance Variables*/
    private int[][] Combinations = new int[][] {
    {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
    {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
    {0, 4, 8}, {2, 4, 6}
    };
    private JFrame window = new JFrame("Frank's Exemplary Tic-Tac-Toe Game of Awesome");
    private JButton buttons[] = new JButton[9];
    private int count = 0;
    private String letter = "";
    private boolean win = false;
    private boolean win2 = false;
    private String player1;
     private String player2;
    public FrankTic(){

    window.setSize(500,500);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLayout(new GridLayout(3,3));


    for(int i=0; i<=8; i++){
    buttons[i] = new JButton();
    window.add(buttons[i]);
    buttons[i].addActionListener(this);


    }


    window.setVisible(true);
    }


    public void actionPerformed(ActionEvent a) {

    count++;


    if(count % 2 == 0){
    letter = "1";
    fillCol = Color.red;
    } else {
    letter = "2";
    fillCol = Color.green;
    }


    JButton pressedButton = (JButton)a.getSource();
    pressedButton.setText(letter);
    pressedButton.setBackground(fillCol);
    pressedButton.setEnabled(false);


    for(int i=0; i<=7; i++){
    if( buttons[Combinations[i][0]].getText().equals(buttons[Combinations[i][1]].getText()) &&
    buttons[Combinations[i][1]].getText().equals(buttons[Combinations[i][2]].getText()) &&
    buttons[Combinations[i][0]].getText() != ""){
    win = true;
    }
    for(int j=0; i<=7; i++){
    if( buttons[Combinations[i][0]].getText().equals(buttons[Combinations[i][1]].getText()) &&
    buttons[Combinations[i][1]].getText().equals(buttons[Combinations[i][2]].getText()) &&
    buttons[Combinations[i][0]].getText() != ""){
    win2 = true;
    }
    **********
    if(win == true){
    JOptionPane.showMessageDialog(null, "Awesome"+ player1 + " wins!");
    System.exit(0);
    }
    if(win2 == true){
    JOptionPane.showMessageDialog(null, "Awesome"+ player2 + " wins!");
    System.exit(0);
    }

    else if(count == 9 && win&&win2 == false){
    JOptionPane.showMessageDialog(null, "Draw game! Play again!");
    System.exit(0);
    }
    }


    }

    }
    public static void main(String[] args){
    FrankTic starter = new FrankTic();


    JOptionPane.showMessageDialog(null, "Please enter your name player 1");
    String player1 = JOptionPane.showInputDialog ( "put your message here" );


    JOptionPane.showMessageDialog(null, "Please enter your name player 2");
    String player2 = JOptionPane.showInputDialog ( "put your message here" );

    }
    }

********

So, the player will play the game after entering their name and instead of the player's name appearing in Messagebox that says "Awesome player's name Wins!", it comes up with "Awesome null wins!". I cannot figure this out for the life of me, please help.

Line 110 you declare a new local variable player1 that "hides" the one you declared on line 31. Don't declare a new variable, use the existing one (ditto for player 2).

Thank you so much! It worked like a charm, I just moved everything to line 31 and 32 and declared the player1 and player2 with the Joptionpane

Ecxellent! don't forget to mark this "solved".

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.