I made a program that utilizes 3 classes, Championship, Game and Club. My methods inside game and club are fine but it's in the main method of Championship class where I'm having issues. I have 2 arraylists, one with objects club and one with objects game. In my main method, I created a menu using a while loop and switch case for the user to 1- create a club, 2- create a game, 3- list results, and 4- register results. However, all my cases have the same issue: the non static variables (arraylists) cannot be accessed in the static context. So, I don't know how to access my arraylists. Everywhere I get an error, there's a comment, but again, they are all the same and all regard the arraylists. Help's appreciated!
Leaving relevant code down here with comments on where Im getting this error.
public class Championship{
ArrayList<Club> clubs;
ArrayList<Game> games;
public Championship() {
clubs = new ArrayList();
games = new ArrayList();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int option = -1;
while(option!=0){
System.out.println("\t1- Create Club");
System.out.println("\t2- Create game");
System.out.println("\t3- List results");
System.out.println("\t4- Register result by index");
option = sc.nextInt();
switch( opçao ){
case 1:
System.out.println("Insert club name.");
String n = sc.nextLine();
sc.nextLine();
System.out.println("Insert club city.");
String c = sc.nextLine();
sc.nextLine();
clubs.add(new Club(n, c)); //ERROR: non static clubs cannot be referenced here in static context
break;
case 2:
Iterator<Club> iter = clubs.iterator(); //ERROR: non static clubs cannot be referenced here in static context
while(iter.hasNext()){
Club a = iter.next();
Club b = iter.next();
if(a != b){
games.add(new Game(a, b)); //ERROR: non static games cannot be referenced here in static context
}
}
break;
case 3:
for(Game game : games){ //ERROR: non static clubs cannot be referenced here in static context
System.out.println(game.getM() + " : " + game.getN());
}
case 4:
System.out.println("Game index");
int s = sc.nextInt();
sc.nextInt();
System.out.println("Goals visited?");
int N = sc.nextInt();
sc.nextInt();
System.out.println("Goals Visitor?");
int M = sc.nextInt();
sc.nextInt();
Iterator<Game> iter2 = games.iterator(); //ERROR: non static clubs cannot be referenced here in static context
while(iter2.hasNext()){
Game j = games.get(s); //ERROR: non static clubs cannot be referenced here in static context
j.setResult(N,M);
}
break;
}
}
public class Club {
private String name;
private String city;
public Clube(String name, String city) {
this.name = name;
this.city = city;
}
public String getCity() {
return city;
}
public String getName() {
return name;
}
}
public class Game{
private Clube visited;
private Clube visitor;
private int N;
private int M;
public Game(Club visited, Club visitor) {
this.visited= visited;
this.visitor= visitor;
}
public void setResult(int N, int M) {
this.N = N;
this.M = M;
}
public int getM() {
return M;
}
public int getN() {
return N;
}