0

I'm a starting programmer and i can't seem to find my mistake in my code.

I have an arraylist of objects(persons) and I want to save the value of an index into a variable:

public void randomCaptain(){
    Random dice = new Random ();
    int n = dice.nextInt(personList.size());
    Person theCaptain = personList.get(n);
    System.out.println(theCaptain);
}

First i want a random number between 1 and the amount of persons in my arraylist. After this I want the value at spot n, so person n of my arraylist and save this into Person 'theCaptain'. I tried this with personList.get(n). But if I check this value by a println it returns 'null'. I checked the size of my array etc and the array is not empty. So that's not the problem.

edit

This is the part where the array is being initialized:

public class Team{
    ArrayList<Person> personList = new ArrayList<Person>();
    void init(){

    //Adding persons to the list

    personList.add(new Coach("Tesan de Boer", "Straatweg 45", 2222));
    personList.add(new GoalKeeper("Peter Post", "Straatweg 45", 2222, 1));
    personList.add(new GoalKeeper("piet puk", "Straatweg 45", 2222, 21));
    personList.add(new GoalKeeper("Siem van Aanhoolt", "Straatweg 45", 2222, 31));
    personList.add(new Captain("Denis van rijn", "Straatweg 45", 2222, 5));
    personList.add(new Fielder("Koen Weegink", "Straatweg 45", 2222, 2));
    personList.add(new Fielder("Jan-Willem Rufus op den Haar", "Straatweg 45", 2222, 3));
    personList.add(new Fielder("Tom Kraniker", "Straatweg 45", 2222, 4));
    personList.add(new Fielder("Leon het Kanon", "Straatweg 45", 2222, 6));
    personList.add(new Fielder("Robin Hogezant", "Straatweg 45", 2222, 7));
    personList.add(new Fielder("Loesoe de Kat", "Straatweg 45", 2222, 8));
    personList.add(new Fielder("Morris de Spee", "Straatweg 45", 2222, 9));
    personList.add(new Fielder("Rein Zoekers", "Straatweg 45", 2222, 10));
    personList.add(new Fielder("Darion Pok", "Straatweg 45", 2222, 11));
    personList.add(new Fielder("Achmed de Bom", "Straatweg 45", 2222, 12)); 
    }

When I check this with size(), than it returns 15 correctly. So that shouldn't be the problem.

In the main:

    Team team= new Team();
        team.init();
        team.randomCaptain();

I hope you can help me, thanks

7
  • 2
    Please write I if it's you and not the famous variable i :) Commented Jun 16, 2013 at 10:10
  • 1
    I don't think the problem is in the part you've posted. We need to see the part where you initialize personList. Commented Jun 16, 2013 at 10:12
  • 2
    You want a number between 1 and the size of personList, bear in mind that to access the list it must be between 0 and personList.size-1, so it must be: int n = dice.nextInt(personList.size())-1; Commented Jun 16, 2013 at 10:22
  • You create a list, OK, but do you feed it with elements? new ArrayList<X>() will not automatically create your X instances on demand! Commented Jun 16, 2013 at 10:23
  • Is it always returning null ? Commented Jun 16, 2013 at 10:39

2 Answers 2

2

I don't see anything wrong with your program. I was able to run your program multiple times and could not reproduce null output.

You can try it out yourself here - http://ideone.com/tVFq4d

Sign up to request clarification or add additional context in comments.

Comments

1

The code that you have posted here seems to work perfectly from what I can tell.

Also you say "I want a random number between 1 and the amount of persons in my arraylist" - do you mean you never want to pick the coach to be the captain? Because the random.nextInt() call can return 0 so look out for that!

import java.util.ArrayList;
import java.util.Random;

public class Test {

    public static void main(String[] args) {
        Team team = new Team();
        team.init();
        team.randomCaptain();
    }
}

class Person {

}

class Coach extends Person {
    public Coach(String string, String string2, int i) {
    }
}

class GoalKeeper extends Person {
    public GoalKeeper(String string, String string2, int i, int j) {
    }
}

class Captain extends Person {
    public Captain(String string, String string2, int i, int j) {
    }
}

class Fielder extends Person {
    public Fielder(String string, String string2, int i, int j) {
    }
}

class Team {
    ArrayList<Person> personList = new ArrayList<Person>();

    void init() {

        // Adding persons to the list

        personList.add(new Coach("Tesan de Boer", "Straatweg 45", 2222));
        personList.add(new GoalKeeper("Peter Post", "Straatweg 45", 2222, 1));
        personList.add(new GoalKeeper("piet puk", "Straatweg 45", 2222, 21));
        personList.add(new GoalKeeper("Siem van Aanhoolt", "Straatweg 45",
                2222, 31));
        personList.add(new Captain("Denis van rijn", "Straatweg 45", 2222, 5));
        personList.add(new Fielder("Koen Weegink", "Straatweg 45", 2222, 2));
        personList.add(new Fielder("Jan-Willem Rufus op den Haar",
                "Straatweg 45", 2222, 3));
        personList.add(new Fielder("Tom Kraniker", "Straatweg 45", 2222, 4));
        personList.add(new Fielder("Leon het Kanon", "Straatweg 45", 2222, 6));
        personList.add(new Fielder("Robin Hogezant", "Straatweg 45", 2222, 7));
        personList.add(new Fielder("Loesoe de Kat", "Straatweg 45", 2222, 8));
        personList.add(new Fielder("Morris de Spee", "Straatweg 45", 2222, 9));
        personList.add(new Fielder("Rein Zoekers", "Straatweg 45", 2222, 10));
        personList.add(new Fielder("Darion Pok", "Straatweg 45", 2222, 11));
        personList.add(new Fielder("Achmed de Bom", "Straatweg 45", 2222, 12));
    }

    public void randomCaptain() {
        Random dice = new Random();
        // Random.nextInt(n) returns a number between 0 (inclusive) and n (exclusive) so it will always pick a valid index in the array list.
        int n = dice.nextInt(personList.size());
        Person theCaptain = personList.get(n);
        System.out.println(theCaptain);
    }
}

This is the output:

Fielder@e83912

I also ran the team.randomCaptain() function in a loop - I ran it over 100 million times and it never assigned null to theCaptain.

3 Comments

thank you very much, I changed my classes exactly to yours, then I added my other methods etc, For some reason I used a ToString that @override and returned null. That was the problem. Pretty dumb..
Haha, damn. Well, you probably won't make that mistake again! :D
indeed, thanks alot though! Now I was able to stop looking at the method itself that apparently already worked.

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.