So basically I have this constructor for the class League:
import java.util.*;
public class League {
private String name;
private List<Team> teamList;
public League(String name) {
List<String> teamNames = new LinkedList<String>(Company.teamList);
Collections.shuffle(teamNames);
teamNames.subList(0, 5);
for(int i = 0; i < teamNames.size(); i++){
teamList.add(new Team(teamNames.get(i)));
}
}
}
The class Company happens to have a Set called teamList.
When I call on System.out.println(teamNames.get(i)) it shows me the content so obviously the elements of the set are there, however when I try to create a new Team object based on the elements of the list of Strings, it gives me a NullPointerException. I don’t know why is that? Help?
Here is the code for the Team class in case you need it:
import java.util.HashMap;
import java.util.Map;
public class Team {
protected Map<Integer, Player> teamPlayerMap;
private String teamName;
public Team(String name) {
teamPlayerMap = new HashMap<Integer, Player>();
teamName = name;
}
public String getTeamName() {
return teamName;
}
}
I think the problem is here:
You need to create an instance of a class that implements
List<Team>and assign it toteamList. You haven’t done this so it will throw aNullPointerExceptionwhen you callteamList.add(...).The fix is to write this instead: