Ok, so I want to start off by saying that I am finishing my Associates in Computer Information Systems, and I AM NOT A PROGRAMMER!! I am not good at it and it is extremely difficult for me to wrap my head around it (which is odd for me because I love to learning; I am learning to speak Russian just for fun).
So, my assignment is to fill in the blanks of code with my own to get the outcome of a tic tac toe game. Here is all of the code:
/* * */ package tictactoe; /** * * @author Gorgo */ public class TicTacToe { // #2. variable and constant declarations - given by book static int [][] gameboard; static final int EMPTY = 0; static final int NOUGHT = -1; //PLAYER O static final int CROSS = 1; //PLAYER X // #3. utility methods - given by book static void set(int val, int row, int col) throws IllegalArgumentException { if (gameboard[row][col] == EMPTY) gameboard[row][col] = val; else throw new IllegalArgumentException ("Player already there!"); } static void displayboard() { for (int r = 0; r < gameboard.length; r++) { System.out.print ("|"); for (int c = 0; c < gameboard[r].length; c++) { switch (gameboard[r][c]) { case NOUGHT: System.out.print("O"); break; case CROSS: System.out.print("X"); break; default: //Empty System.out.print(" "); } System.out.print("|"); } System.out.println("\n--------\n"); } } // #5. define createBoard method - MY CODE static void createBoard(int rows, int cols) { //#6. Initialize gameboard int[][] createBoard = new int[rows][cols]; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { createBoard[rows][cols] = -1; } } } //#6. define winOrTie method - MY CODE static boolean winOrTie() { // #6. determine whether X or O won or there is a tie - MY CODE int winner = -2; for (int r = 0; r < 3; r++) if ((gameboard[r][0] == winner) && (gameboard[r][1] == winner) && (gameboard[r][2] == winner)) { return true; } for (int j = 0; j < 3; j++) if ((gameboard[0][j] == winner) && (gameboard[1][j] == winner) && (gameboard[2][j] == winner)) { return true; } if ((gameboard[0][0] == winner) && (gameboard[1][1] == winner) && (gameboard[2][2] == winner)) { return true; } if ((gameboard[0][2] == winner) && (gameboard[1][1] == winner) && (gameboard[2][0] == winner)) { return true; } return false; } public static void main(String[] args) { //#8. contents of main() - given by book displayboard(); int turn = 0; int playerVal; int outcome; java.util.Scanner scan = new java.util.Scanner(System.in); do { playerVal = (turn % 2 == 0) ? NOUGHT: CROSS; if (playerVal == NOUGHT) System.out.println ("\n-O's turn-"); else System.out.println ("\n-X's turn-"); System.out.print ("Enter row and column:"); try { set (playerVal, scan.nextInt(), scan.nextInt()); } catch (Exception ex) {System.err.println(ex);} turn++; outcome = winOrTie(); } while ( outcome == -2 ); displayboard(); switch (outcome) { case NOUGHT: System.out.println("O wins!"); break; case CROSS: System.out.println("X wins!"); break; case 0: System.out.println("Tie."); break; } } }
So, the errors that I am getting are:
#1. "Exception in thread "main" java.lang.NullPointerException"
this is pointing to the 'displayboard' method:
and here:static void displayboard() { for (int r = 0; r < gameboard.length; r++) { //here at this line System.out.print ("|");
public static void main(String[] args) { //#8. contents of main() displayboard(); //here at this line int turn = 0; int playerVal; int outcome;
My problem that I am having with these errors is: 1. these particular lines of code were given to me; I did not write them; 2. I don't understand why it is pointing at the first bit. Everything that I have read in my text book says the exact same code.
Therefore, my question is why is this one line (the 'displayboard method') an error.
Thank you all in advance