Leon Pater
Leon Pater

Reputation: 19

How do i save a value from an object array in java?

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

Upvotes: 0

Views: 426

Answers (2)

srikanta
srikanta

Reputation: 2999

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

Upvotes: 2

Wayne Uroda
Wayne Uroda

Reputation: 5095

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.

Upvotes: 1

Related Questions