Question

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); 


}

   }
Was it helpful?

Solution

Assuming that the NPE is occuring at this line (and I think it is)

    for (int i=0; i<division.getNumTeams();i++){

it is happening because division is null. That is the only possible cause for an NPE to be thrown at that point.

And my reading of your code is that you have not initialized it, so it should be null.

Note that the code in main that creates 2 Division objects does NOT initialize the division instance variable. Look at the code carefully.

OTHER TIPS

Which line throws the NPE? It's not just "the for loop", it's a specific line, and that line is very important. I'm guessing it's the first line of the for loop and it's because the division variable is never initialized. If so, initialize it before using it.


More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). You should inspect the line carefully that throws it, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top