I'm doing an exercise in decomposition. Here is the situation. I have one LEAGUE consisting of a number of DIVISIONS which in turn consists of a number of TEAMS which consists of PLAYERS. I have a class LeagueStatistics that has a method getAveAgeOfTeam where the name of a team is passed as a string in the argument. The problem that I am having is that the values I set for the objects in the Test class aren't being recognised in the LeagueStatistics class, even though they have been set in the relevent classes that it is composed of. It is returning a value of 0. I think this is because I have created new instances of the objects for league, division etc in the LeagueStatistics class but if I don't create new instances here then I get a null pointer exception. I can't figure out why it's not working so any help is appreciated!
public class LeagueStatistics {
private static LeagueStatistics instance;
private int numDivisions;
private int numTeams;
private double aveGoalsForDivision;
private double aveGoalsForTeam;
private double aveAgeOfTeam;
private League league=new League();
private Division division=new Division();
private Team team=new Team();
private Player player=new Player();
public LeagueStatistics() {
}
public static LeagueStatistics getInstance(){
if (instance==null){
instance=new LeagueStatistics();
}
return instance;
}
public double getAveAgeOfTeam(String teamName){
ArrayList<Team> myList = new ArrayList<Team>();
*************************************************
for (int i=0; i<division.getNumTeams();i++){
team=division.getTeamList().get(i);
if (team.equals(teamName)){
myList.add(team);
}
ArrayList<Player> myPlayerList=new ArrayList<Player>();
for (int j=0; j<myList.size(); j++){
player=team.getPlayerList().get(j);
myPlayerList.add(player);
}
int age=0;
for(int k=0; k<myPlayerList.size(); k++){
int playerAge=myPlayerList.get(k).getAge();
age+=playerAge;
}
aveAgeOfTeam=(age/myPlayerList.size());
}
return aveAgeOfTeam;
}
public class Tester {
public static void main(String[] args) {
Player player1=new Player();
player1.setAge(15);
player1.setGoals(12);
Player player2=new Player();
player2.setAge(19);
player2.setGoals(6);
Player player3=new Player();
player3.setAge(17);
player3.setGoals(9);
Player player4=new Player();
player4.setAge(10);
player4.setGoals(19);
Team team1=new Team();
team1.setTeamName("Team1");
team1.setTrophies(2);
Team team2=new Team();
team2.setTeamName("Team2");
team2.setTrophies(3);
Team team3=new Team();
team3.setTeamName("Team3");
team3.setTrophies(1);
ArrayList<Player> ListOfPlayers=new ArrayList<Player>();
ListOfPlayers.add(player1);
ListOfPlayers.add(player2);
team1.setPlayerList(ListOfPlayers);
ArrayList<Player> ListOfPlayers2=new ArrayList<Player>();
ListOfPlayers2.add(player3);
team2.setPlayerList(ListOfPlayers2);
ArrayList<Player> ListOfPlayers3=new ArrayList<Player>();
ListOfPlayers3.add(player4);
team3.setPlayerList(ListOfPlayers3);
Division division1=new Division();
division1.setDivisionName("Division1");
Division division2=new Division();
division2.setDivisionName("Division2");
ArrayList<Team> ListOfTeams=new ArrayList<Team>();
ListOfTeams.add(team1);
ListOfTeams.add(team2);
division1.setTeamList(ListOfTeams);
ArrayList<Team> ListOfTeams2=new ArrayList<Team>();
ListOfTeams2.add(team3);
division2.setTeamList(ListOfTeams2);
League league=new League();
league.setLeagueName("Premier");
ArrayList<Division> ListOfDivisions=new ArrayList<Division>();
ListOfDivisions.add(division1);
ListOfDivisions.add(division2);
league.setDivisionList(ListOfDivisions);
Double avgAge=LeagueStatistics.getInstance().getAveAgeOfTeam("Team1");
System.out.println(avgAge);
}
}