I have the following code and keep getting a null pointer exception at the last line of this code. I cannot figure out why. If anyone can help me out it would be appreciated
This is my class where it messes up. It messes up when trying to load the temp variable into the matchups array list. I'll also show the part in main where I use it below
package generator; import java.util.ArrayList; import java.util.Random; import matchups.Matchups; import teams.Teams; public class Generator { private ArrayList<Matchups> matchups; private ArrayList<Teams> usedTeams; Random rand = new Random(); Matchups temp; private int id; int i; public Generator(ArrayList<Matchups> list, int num){ this.id = num; temp = list.get(rand.nextInt(list.size())); matchups.add(temp); usedTeams.add(temp.getTeamOne()); usedTeams.add(temp.getTeamTwo()); for(i = 0; i < 4; i++){ do{ temp = list.get(rand.nextInt(list.size())); }while(usedTeams.contains(temp.getTeamOne()) || usedTeams.contains(temp.getTeamTwo())); usedTeams.add(temp.getTeamOne()); usedTeams.add(temp.getTeamTwo()); matchups.add(temp); } } public int getId(){ return this.id; } public ArrayList<Matchups> getWeek(){ return this.matchups; } }
Main:
Matchups current; ArrayList<Matchups> matchups = new ArrayList<Matchups>(); for(i = 0; i < 10; i++){ for(j = i + 1; j < 10; j++){ matchups.add(new Matchups(teams.get(i), teams.get(j))); } } System.out.println("WEEK ONE"); Generator weekOne; weekOne = new Generator(matchups, 1);