为什么测试类中设置的对象实例未实现?

时间:2014-03-22 02:09:55

标签: java decomposition

我正在进行分解练习。情况就是这样。我有一个由许多DIVISIONS组成的LEAGUE,而DIVISIONS又由一些由PLAYERS组成的TEAMS组成。我有一个类LeagueStatistics,它有一个方法getAveAgeOfTeam,其中团队的名称作为参数中的字符串传递。我遇到的问题是我在LeagueStatistics类中没有识别为Test类中的对象设置的值,即使它们已经在由它组成的相关类中设置。它返回值0.我认为这是因为我在LeagueStatistics类中为联盟,分区等创建了对象的新实例但是如果我不在这里创建新实例那么我得到一个空指针异常。我无法弄清楚为什么它不起作用所以任何帮助都表示赞赏!

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


}

   }

2 个答案:

答案 0 :(得分:1)

假设NPE出现在这一行(我认为是)

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

它正在发生,因为divisionnull。这是NPE被抛出的唯一可能原因。

我对您的代码的解读是您尚未对其进行初始化,因此 应为null

请注意,main中创建2个Division对象的代码不会初始化division实例变量。仔细查看代码

答案 1 :(得分:0)

哪条行抛出NPE?它不仅仅是&#34; for循环&#34;,它是一条特定的行,而且该行 非常 很重要。我猜测它是for循环的第一行,因为分割变量从未被初始化。如果是这样,请在使用之前对其进行初始化。


更重要的是,您需要学习如何调试NPE(NullPointerException)的一般概念。您应仔细检查抛出它的行,找出哪个变量为null,然后追溯到您的代码以查看原因。你会一次又一次地碰到这些,相信我。