0

So below we have the code from the first of my two files, titled Game (keep scrolling down to see the second file + the issue)

public class Game
{
    private static final int PLAYING=0;
    private static final int RED_WINS=1;  
        private static final int BLACK_WINS=2;
        private static final int DRAW=3;
            private static final int RED=4;
            private static final int BLACK=5;
            private static final int EMPTY=6;
        private static final int WALL=7;
    private static int board[][];

public Game()
{


    int board[][]={{WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
                {WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
                {WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
                {WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
                {WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL},
                {WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL,EMPTY,WALL}};


}
public  void draw()
{
    for(int r= 0; r< board.length; r++)
        {

            for(int c= 0; c<board[0].length; c++)
                {
                    if(board[r][c]==WALL)
                    {
                        char Wall='|';
                        board[r][c]=Wall;
                    }
                        if(board[r][c]==EMPTY)
                    {
                        char Empty=' ';
                        board[r][c]=Empty;
                    }
                    if(board[r][c]==BLACK)
                    {
                        char Black='B';
                        board[r][c]=BLACK;
                    }
                    if(board[r][c]==RED)
                    {
                        char Red='R';
                        board[r][c]=RED;
                    }
                    System.out.print(board[r][c] + "");
                }
                System.out.print("\n");
       }
}
}

And Below is the code from the second file, MainFile

import java.util.Scanner;
public class MainFile
{
    public static void main(String[]args)
    {
        Game a=new Game();
        a.draw();
    }
}

When I press compile, the output says Process completed and everything seems fine at first, but when I press run file I get the following runtimerror,could I know what I'm doing wrong and how can I fix it?

Exception in thread "main" java.lang.NullPointerException
    at Game.draw(Game.java:28)
    at MainFile.main(MainFile.java:7)
Milan
  • 1
  • 1
  • Note: JavaScript and Java have very little to do with one another. I've removed the irrelevant tag. – T.J. Crowder Dec 10 '16 at 19:04
  • Cut your board[][] array and make it local variable with its elements. You declared it firstly, but didn't fill. like this: private static int board[][] = {{a,b,c},{a,b,c},{a,b,c}} – JavadKhan Dec 10 '16 at 19:31

0 Answers0