1

I have three classes. The Player Class:

public class Player
{
private String playerName;
private int playerWorth;
private int playerSpent;
// A list of player prize objects.
private ArrayList<Prize> playerPrizeList;

public void Player()
{
     playerPrizeList = new ArrayList <Prize>();
     playerName = "";
     playerWorth = 0;
     playerSpent = 0;
}

public void playerDetails(String playerName, int playerWorth, int playerSpent)
{
    setPlayerName(playerName);
    setPlayerWorth(playerWorth);
    setPlayerSpent(playerSpent);
}

public void prize(String prizeName, int prizeWorth, int prizeCost)
{
    populatePrize(prizeName, prizeWorth, prizeCost);
}

private void populatePrize(String prizeName, int prizeWorth, int prizeCost)
{
    playerPrizeList.add(new Prize(prizeName, prizeWorth, prizeCost));
}

The other class is PlayerList class from within which I am trying to add a new player object to an arrayList which works but then I also have a prizeList Array of (Prize Objects) inside this Player class. Once I add the player to the ArrayList how do I retrieve that object and make changes to (my last block of code). I hope I explained that properly.

public class PlayerList
{
private ArrayList<Player> playerAList; 

public PlayerList() 
{
    playerAList = new ArrayList<Player>();
}

public ArrayList<Player> addPlayer(String playerName, int playerWorth, int playerSpent)
{
    Player newPlayer = new Player();
    newPlayer.setPlayerName(playerName);
    newPlayer.setPlayerWorth(playerWorth);
    newPlayer.setPlayerSpent(playerSpent);
    playerAList.add(newPlayer);
    return playerAList;
}

/**
 * @i is the index of ArrayList for Player Prizes
 */
public void setPlayerPrizeList(int i, String prizeName, int prizeWorth, int prizeCost)
{
    playerAList.get(i) =  currentPlayer;
    currentPlayer.populatePrize(prizeName, prizeWorth, prizeCost);
    playerAList.set(i, currentPlayer);
    return playerAList;
}
5
  • What exactly is the problem you have with your current code? Commented May 23, 2015 at 23:55
  • the setPlayerPrizeList is not doing what it's supposed to which is get an object with index i from the playerAList then add a new object (Prize) to it with prizeName/prizeWorth/prizeCost Commented May 23, 2015 at 23:58
  • Does this line even compile? playerAList.get(i) = currentPlayer; Well, have a look at my answer below. Commented May 24, 2015 at 0:01
  • Why does a void method return an array list? Commented May 24, 2015 at 0:19
  • @BenKnoble good pickup sir. I have removed void and is now working like a charm. Commented May 24, 2015 at 0:59

1 Answer 1

1

In general, your approach looks good.

There are a few issues in your code however:

  1. You cannot assign values to a method call.

    playerAList.get(i) =  currentPlayer;
    

    This should work, however:

    currentPlayer = playerAList.get(i);
    
  2. Besides that, you do not need this line:

    playerAList.set(i, currentPlayer);
    

    The Player you are changing in this method stays in the list and is "updated automatically", since all the changes are executed on the same memory block, the one your Player has been created in. Both your playerAList and the currentPlayer hold a reference to this memory block.

  3. One more thing is wrong. You can't return anything from a method with return type void:

    return playerAList;
    

All in all, your method setPlayerPrizeList() should look like this:

public void setPlayerPrizeList(int i, String prizeName, int prizeWorth, int prizeCost) {
    currentPlayer = playerAList.get(i);
    currentPlayer.populatePrize(prizeName, prizeWorth, prizeCost);
}
Sign up to request clarification or add additional context in comments.

1 Comment

very good answer @TimoSta . That compiled but now giving me null pointer exception error. Looks like i am not instantiating my ArrayList playerPrizeList. I guess before i populatePrize i need to somehow do that right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.