Z
zcraven
public void createFixtureList()
{
//setup 'cal' to get todays date
Calendar cal = new GregorianCalendar();
int y = cal.get(Calendar.YEAR);
int m = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ...
m++; // 1=Jan, 2=Feb,
....
int d = cal.get(Calendar.DAY_OF_MONTH);
int hr = 15;
int min = 00;
int j = league.size();
j--; // last club in league will have already had fixtures
generated by previous clubs
for (int i=0; i<j; i++)
{
//get a CLUB(i) and create all possible fixtures for it
Club c1 = (Club)league.get(i);
for (int x=i+1; x<league.size(); x++)
{
// get an opponent club - CLUB(x) to create 2 fixtures
(home/away) with club(i)
Club c2 = (Club)league.get(x);
// HOME GAME - CLUB(i) vs CLUB(x)
// set game date for one week after the previous club(i)
matchdate
d = d+7;
if (d > 23) // correct values 31->1
{
m++;
d=(d-30);
if (m > 11) // correct values dec->jan
{
m = 1;
y++;
}
}
Calendar homedate = new GregorianCalendar(y, m, d, hr, min);
Fixture f1 = new Fixture(c1.getGround(), homedate, c1, c2);
fixtures.add(f1);
// AWAY GAME CLUB(x) vs CLUB(i)
// set away game for 6 months later
int tempM = m;
int tempY = y;
m = (m+6);
if (m > 12)
{
m=(m-12);
y=y+1;
}
Calendar awaydate = new GregorianCalendar(y, m, d, hr, min);
Fixture f2 = new Fixture(c2.getGround(), awaydate, c2, c1);
fixtures.add(f2);
// return m and y back to previous CLUB(i) homegame value
m = tempM;
y = tempY;
homedate.clear();
homedate.set(y,m,d,hr,min);
}
}
printFixtureList();
}
For some reason, the above code worked perfectly when I had 3 clubs in the
league. But since I added a 4th club, I always get a runtime error.
It generates all fixtures (8) for the first club (club(i)) perfectly. But
the problem occurs when it tries to increment i and get the next club to be
club(i). When it tries to get the club, it throws a 'null pointer
exception'. So the problem seems to be with the first FOR statement in the
above code.
{
//setup 'cal' to get todays date
Calendar cal = new GregorianCalendar();
int y = cal.get(Calendar.YEAR);
int m = cal.get(Calendar.MONTH); // 0=Jan, 1=Feb, ...
m++; // 1=Jan, 2=Feb,
....
int d = cal.get(Calendar.DAY_OF_MONTH);
int hr = 15;
int min = 00;
int j = league.size();
j--; // last club in league will have already had fixtures
generated by previous clubs
for (int i=0; i<j; i++)
{
//get a CLUB(i) and create all possible fixtures for it
Club c1 = (Club)league.get(i);
for (int x=i+1; x<league.size(); x++)
{
// get an opponent club - CLUB(x) to create 2 fixtures
(home/away) with club(i)
Club c2 = (Club)league.get(x);
// HOME GAME - CLUB(i) vs CLUB(x)
// set game date for one week after the previous club(i)
matchdate
d = d+7;
if (d > 23) // correct values 31->1
{
m++;
d=(d-30);
if (m > 11) // correct values dec->jan
{
m = 1;
y++;
}
}
Calendar homedate = new GregorianCalendar(y, m, d, hr, min);
Fixture f1 = new Fixture(c1.getGround(), homedate, c1, c2);
fixtures.add(f1);
// AWAY GAME CLUB(x) vs CLUB(i)
// set away game for 6 months later
int tempM = m;
int tempY = y;
m = (m+6);
if (m > 12)
{
m=(m-12);
y=y+1;
}
Calendar awaydate = new GregorianCalendar(y, m, d, hr, min);
Fixture f2 = new Fixture(c2.getGround(), awaydate, c2, c1);
fixtures.add(f2);
// return m and y back to previous CLUB(i) homegame value
m = tempM;
y = tempY;
homedate.clear();
homedate.set(y,m,d,hr,min);
}
}
printFixtureList();
}
For some reason, the above code worked perfectly when I had 3 clubs in the
league. But since I added a 4th club, I always get a runtime error.
It generates all fixtures (8) for the first club (club(i)) perfectly. But
the problem occurs when it tries to increment i and get the next club to be
club(i). When it tries to get the club, it throws a 'null pointer
exception'. So the problem seems to be with the first FOR statement in the
above code.