Im trying to do a tactoe game, but im having some trobles. Im a begginer so i might have done someting silly but im dumb yet lol. If you could help me, I'd be thankfull. So in the main code, I should use Visual as an matrix and itwould be shown at the drawgame method, but theres a problem about nullpointerexception. I searched and found that the problem is probably related to the initialization of the matrix...
This is the error:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.company.Visual.getSymbol()" because "visual[0][0]" is null
at com.company.Main.drawGame(Main.java:48)
at com.company.Main.main(Main.java:21)
This is the code of the Main.java (main class):
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
//variables
Visual[][] visual = new Visual[3][3];
//public static Visual[3][3] visual = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
char symbol='X';
boolean game = true;
String victory="";
Scanner scan = new Scanner(System.in);
//int line=0,colunm=0;
//game
while(game){
if(visual!=null)drawGame(visual);
//drawGame(visual);
victory = verifyVictory(visual);
if(victory.equals("")){
System.out.printf("Player %s won!", victory);
break;
}
try {
whoPlays(visual, play(scan, symbol), symbol);
if(symbol=='X'){
symbol='O';
}else{
symbol='X';
}
} catch (Exception e) {
System.out.println("Error!");
}
}
System.out.println("End");
}
//function to call the Visual class and show the visual of tactoe
public static void drawGame(Visual[][] visual){
System.out.printf(" 0 1 2\n");
System.out.printf("0 %c | %c | %C \n", visual[0][0].getSymbol(),visual[0][1].getSymbol(),visual[0][2].getSymbol());
System.out.printf(" --------------\n");
System.out.printf("1 %c | %c | %C \n",visual[1][0].getSymbol(),visual[1][1].getSymbol(),visual[1][2].getSymbol());
System.out.printf(" --------------\n");
System.out.printf("2 %c | %c | %C \n",visual[2][0].getSymbol(),visual[2][1].getSymbol(),visual[2][2].getSymbol());
}
//clean the old drawGame()
public static void clean(){
for(int i=0;i<50;i++){
System.out.println("");
}
}
//coords to play
public static int[] play(Scanner scan, char symbol){
int p[] = new int[2];
System.out.printf("%s %c\n", "Who plays: ", symbol);
System.out.print("Line: ");
p[0] = scan.nextInt();
System.out.print("Colunm: ");
p[1] = scan.nextInt();
return p;
}
//who is playing
public static boolean whoPlays(Visual[][] visual, int p[], char symbol){
if(visual[p[0]][p[1]].getSymbol()==' '){
visual[p[0]][p[1]].setSymbol(symbol);
return true;
}else{
return false;
}
}
//this is an attempt to inicialize the array visual, since I saw this might be the problem
public static void start(Visual[][] visual){
for(int i=0;i<3;i++){
for(int c=0;c<3;c++){
visual[i][c] = new Visual();
}
}
}
//still doing this, useless by now
public static String verifyVictory(Visual[][] visual){
return "";
}
}
This is the code of the Visual.java (secondary class)
package com.company;
public class Visual {
private char symbol;
public Visual(){
this.symbol=' ';
}
public char getSymbol(){
return this.symbol;
}
public void setSymbol(char symbol){
if(this.symbol==' '){
this.symbol=symbol;
}else{
System.out.println("Error");
}
}
}