-1

I am prompting the user to enter their name as well as adding an ai player to my ArrayList. Unfortunately, I am receiving a "cannot find symbol" error on the words "add" in my code below. I am assuming it's something along the lines of not importing something, I can't seem to quite figure it out at the moment.

package connectfour;

import java.util.ArrayList;
import javax.swing.*;
import userInterface.Connect4Ui;
import core.Player;
import core.HumanPlayer;
import core.AiPlayer;


/**
 *
 * @author j_ortiz9688
 */

// connect 4 main 
public class ConnectFour {

    private static ArrayList<Player>player;
    private static Connect4Ui frame; 

    public static void makePlayers(){

        player = new ArrayList<Player>();

        String name = JOptionPane.showInputDialog("Enter your name");

        HumanPlayer player = new HumanPlayer(name);


        AiPlayer ai = new AiPlayer("Computer", 0); 

        player.add(player); 
        player.add(ai);

    }



    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        frame = new Connect4Ui(); 

    }

}
Leopold Stoich
  • 467
  • 1
  • 4
  • 11

2 Answers2

5

You are creating a new variable with the same name as the ArrayList, which hides the static ArrayList field in the class (It won't compile even if you remove the calls to .add()). You will need to rename either the local player variable or the player field.

For example:

    HumanPlayer humanPlayer = new HumanPlayer(name); // Renamed
    AiPlayer aiPlayer = new AiPlayer("Computer", 0); 

    player.add(humanPlayer); 
    player.add(aiPlayer);
SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
2

You define a local variable HumanPlayer player = new HumanPlayer(name); that hides your ArrayList datamember of the same name. Just give it a different name, and you should be OK:

HumanPlayer human = new HumanPlayer(name); // Here!
AiPlayer ai = new AiPlayer("Computer", 0); 

player.add(human);  // and use it here, of course
player.add(ai);
Mureinik
  • 297,002
  • 52
  • 306
  • 350