Vswe's Summer Courses | You're not logged in. Sign up or log in | Server time: | Donate
One can now see other persons' badge hunts. Simply head over to their profiles to access them.

Go back to course page

Thinking with objects

Object orientated programming in Java

BotPlayer method crashing in loop

  • 2013-08-07 01:22:50
    I'm trying to loop through each bot player and print out its score and name at the beginning of each round, but it crashes the game after looping through once.

    (I left out a lot of methods that aren't causing the crash, such as the nextValidInt)
    This is the loop that creates the bot players:
    System.out.println("How many players? (2-6)");
    int botCount = myScanner.nextInt() - 1;
    BotPlayer[] botPlayers = new BotPlayer[botCount];
    ControlledPlayer player1 = new ControlledPlayer(1);
    for (int i = 0; i < botPlayers.length; i++) {
    botPlayers[i] = new BotPlayer(i += 2);
    }

    These are the Player and BotPlayer classes:

    public abstract class Player implements ICardHolder {

    int points = 0;
    String name;

    public Player(int nameNumber) {
    String name = "Player " + nameNumber;
    this.name = name;
    }

    public String getName(){
    return this.name;
    }

    public void playCard() {

    }

    public int getPoints() {
    return points;
    }
    }

    public class BotPlayer extends Player {

    public BotPlayer(int name) {
    super(name);
    }

    @Override
    public String getName(){
    return super.getName();
    }

    @Override
    public int getPoints(){
    return 3;
    }

    }

    And this is the loop that crashes the game:
    round += 1;
    System.out.println("Round " + round);
    System.out.println(player1.getName() + "(Player): " + player1.getPoints() + " points");
    for (int i = 0; i < botPlayers.length; i++) {
    System.out.println(botPlayers[i].getName() + "(Bot): " + botPlayers[i].getPoints() + " points");
    }
  • 2013-08-07 03:18:24
    Just to clarify, the loop works perfectly for the first bot, then crashes at the second bot. When there's only one bot, the game doesn't crash.
  • 2013-08-07 03:23:24
    what error did you get?
  • 2013-08-07 04:02:01
    This one:

    java.lang.NullPointerException
        at Game.main(Game.java:20)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
  • 2013-08-07 04:02:43
    NullPointerException means something's missing, right?
  • 2013-08-07 04:15:03
    yes, nullpointerexception means that you did not initialize your variables properly.

    the problem lies in this line :
    botPlayers[i] = new BotPlayer(i += 2);

    the problem with this code is that you increase the value of i whenever you create a new BotPlayer.

    it should be:
    botPlayers[i] = new BotPlayer(i + 2);
  • 2013-08-07 04:31:12
    Thanks! Guess I should revisit some of the earlier lectures.
  • 2013-08-07 13:56:13
    Another tip is to have one array/list with all the players. Don't treat the bots different from the player.
  • 2013-08-07 22:04:49
    The reason I have the BotPlayers separate from the player is because when I use an ArrayList I need to individually name each object I put in, and I don't know how to do that when creating a varying amount of objects. Is there a way to do that that I missed in the lecture?
  • 2013-08-07 22:08:16
    What do you mean by "individually name each object"?
  • 2013-08-07 22:24:04
    I mean I have to individually create each botplayer so I can add them to the array, but since I want the user to determine the number of bots, I have them created by a loop, and I need to name an object when creating it (BotPlayer botplayer1 = new BotPlayer()) but I don't know how to name objects in a loop.
  • 2013-08-07 22:25:48
    However, by having the BotPlayers in a separate BotPlayer array, I don't need to name them.
  • 2013-08-08 00:01:18
    You don't name objects, just put them in the array of players.
  • 2013-08-08 00:01:26
    i don't really understand what you are talking about but you can create an arraylist of players, add controlledplayer as the first one and then add the rest of the botplayers using a for loop the same way.
  • 2013-08-08 01:00:49
    Sorry, when I first tried storing the ControlledPlayer and BotPlayers together in a Player array, it didn't work, which is why I separated them. It works fine now, I must have done something wrong before.
  • 2013-09-02 10:09:27
    I just kept them separate because it was convienient :P
  • 2013-09-02 10:46:03
    Don't separate them. You will probably have to re-do it to pass the assignment if you do so.
  • Log in or sign up to reply to this thread