Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: first consul game in java (tic tace): problem with the winner check method

  1. #1
    Junior Member
    Join Date
    May 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default first consul game in java (tic tace): problem with the winner check method

    Hey,

    This is my first seruis code,
    I will be glad if you first help me solved the problem, and you are welcome to give me general tips

    When the "winner checker" func that I build is running the program is collapsing.

    https://pastebin.com/avdTkAC6
    xception in thread "main" java.lang.NullPointerException
    	at starting.Lerning_Arrays.winnerCheker(Lerning_Arrays.java:78)
    	at starting.Lerning_Arrays.main(Lerning_Arrays.java:32)
    Thanks Adam.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: first consul game in java (tic tace): problem with the winner check method

    Exception in thread "main" java.lang.NullPointerException
    at starting.Lerning_Arrays.winnerCheker(Lerning_Array s.java:78)
    Look at the statement at line 78 and try to find the variable with the null value. Then backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: first consul game in java (tic tace): problem with the winner check method

    Quote Originally Posted by Norm View Post
    Look at the statement at line 78 and try to find the variable with the null value. Then backtrack in the code to see why that variable does not have a valid value.
    Hey,
    I have checked and I initialize the variable that was null this problem is solved/

    it's still doesn't find the winner the func is now working

    This is the problematic function:
    static String winnerCheker() {
    		String answer = "";
    		for(int a = 0 ; a < 8 ; a++) {
    			String line = "";
    			switch (a) {
    				case 0:
    					line = board[0][0] + board[0][1] + board[0][2];
    					break;
    				case 1:
    					line = board[1][0] + board[1][1] + board[1][2];
    					break;
    				case 2:
    					line = board[2][0] + board[2][1] + board[2][2];
    					break;
    				case 3:
    					line = board[0][0] + board[1][0] + board[2][0];
    					break;
    				case 4:
    					line = board[0][1] + board[1][1] + board[1][2];
    					break;
    				case 5:
    					line = board[1][2] + board[0][2] + board[2][2];
    					break;
    				case 6:
    					line = board[0][0] + board[1][1] + board[2][2];
    					break;
    				case 7:
    					line = board[0][2] + board[1][1] + board[2][0];
    					break;
    			}
    			if (line.equals("XXX")) {
    				answer = line;
    			} else if (line.equals("OOO")) {
    				answer = line;
    			}	
    		}
    		return answer;
    	}
    Last edited by Norm; May 20th, 2019 at 03:23 PM. Reason: Change Quote tag to code tag

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: first consul game in java (tic tace): problem with the winner check method

    What is the statement on line 78 where the exception happened?
    What variable on that line has the null value?
    What statement gave a value to that variable?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2019
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: first consul game in java (tic tace): problem with the winner check method

    Quote Originally Posted by Norm View Post
    What is the statement on line 78 where the exception happened?
    What variable on that line has the null value?
    What statement gave value to that variable?
    78: line = board[0][0] + board[0][1] + board[0][2];
    all variable as initialized

    static String line = "1"; //I initialized its with 1 just for its no to be null
    public static String[][] board; // this is the main array for the game
    	static String winnerCheker() {
    		for(int a = 0 ; a < 8 ; a++) {
    			switch (a) {
    				case 0:
    					line = board[0][0] + board[0][1] + board[0][2];
    					break;
    				case 1:
    					line = board[1][0] + board[1][1] + board[1][2];
    					break;
    				case 2:
    					line = board[2][0] + board[2][1] + board[2][2];
    					break;
    				case 3:
    					line = board[0][0] + board[1][0] + board[2][0];
    					break;
    				case 4:
    					line = board[0][1] + board[1][1] + board[1][2];
    					break;
    				case 5:
    					line = board[1][2] + board[0][2] + board[2][2];
    					break;
    				case 6:
    					line = board[0][0] + board[1][1] + board[2][2];
    					break;
    				case 7:
    					line = board[0][2] + board[1][1] + board[2][0];
    					break;
    			}
    			if (line.equals("XXX")) {
    				return line;
    			} else if (line.equals("OOO")) {
    				return line;
    			}	
    		}
    		return line;
    	}

    Thank you!!!
    Last edited by BlacKDay; May 21st, 2019 at 12:53 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: first consul game in java (tic tace): problem with the winner check method

    Sorry, I still do not understand what variable has the null value.
    Can you give the name of the variable with the null value?

    If you can not tell which variable has the null value, add a print statement just before line 78 that prints the values of the variables on line 78.

    78: line = board[0][0] + board[0][1] + board[0][2];
    What values are in board[0][0] and board[0][1] and board[0][2]?

    Where is the variable: board assigned a value?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. word game (problem how to check that the word is in the dictionary)
    By Rabia in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 28th, 2014, 10:05 AM
  2. Replies: 1
    Last Post: April 12th, 2013, 10:25 AM
  3. [SOLVED] Tic Tac Toe - Checking for a winner
    By FrederikB in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 4th, 2013, 11:04 AM
  4. Tic-Tac-Toe array Win Check
    By zwigoose in forum Loops & Control Statements
    Replies: 6
    Last Post: October 28th, 2012, 06:42 AM
  5. tic tac toe game problem with X and O
    By woohooXX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 20th, 2011, 07:34 AM