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 7 of 7

Thread: Run() method of runnable function has no access to data members?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Run() method of runnable function has no access to data members?

    I'm trying to make a server-client based TicTacToe game, and here I'm just starting off with the raw, raw basics.

    The "client" will be executed in a thread. So, it implements runnable and has a Run() function.

    I want to print the board during this run function.....but it claims that my board hasn't been initialized!!!

    I've bolded the "board.printBoard()" command in the two parts that I have it in my code. The first part is part of the constructor, and it prints the board with no problem.

    Once I enter the run() function, however, it started throwing exceptions all of a sudden for no apparent reason. What's going on here?

    Here's the error message:

    HTML Code:
    Let's try to print the board
    Exception in thread "main" java.lang.NullPointerException
            at TTTClient.run(TTTClient.java:53)
            at TTTClient.<init>(TTTClient.java:47)
            at TTTClient.main(TTTClient.java:27)

    And here's the code.

    Thanks in advance for the help.

    import java.io.*;
    import java.net.*;
    import java.util.concurrent.*;
    import java.util.Scanner;
     
     
    public class TTTClient implements Runnable
    {
    	private char mySymbol;
    	private boolean myTurn;
    	private final char X_SYMBOL = 'X';
    	private final char O_SYMBOL = 'O';
     
    	private Board board;
    	private Socket connection;
     
    	private DataInputStream inpFromServer;
    	private DataOutputStream outToServer;
     
    	public static void main(String[] args)
    	{
    		TTTClient player = new TTTClient();	
    	}
     
    	public TTTClient()
    	{
    		Board board = new Board();
    		[B]board.printBoard();[/B]
     
    		try
    		{
    			connection = new Socket("localhost", 9821);
     
    			inpFromServer = new DataInputStream(connection.getInputStream());
                            outToServer = new DataOutputStream(connection.getOutputStream());
    		}
    		catch (IOException ex)
    		{
    			System.err.println(ex);
    		}
     
    		run();
    	}
     
    	public void run()
    	{
    		System.out.println("Let's try to print the board");
    		[B]board.printBoard();[/B]
    		System.out.println("The board should have been printed");
             }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Run() method of runnable function has no access to data members?

    Exception in thread "main" java.lang.NullPointerException
    at TTTClient.run(TTTClient.java:53)
    At line 53 when the code was being executed there was a variable with a null value. Find the variable and then back track 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
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Run() method of runnable function has no access to data members?

    Well....the variable was the 'board'.

    When I backtrack, I see that the 'board' was set to a 'new Board', thus eliminating its null value.

    I verified this by calling the printBoard method, via 'board', in the constructor (right after board = new Board()).

    Hence, the board is no longer null.

    Yet, when I enter the run function, it magically becomes null once again. I have no idea why. As a data member of my TTTClient class, I see no reason why initializing it in the constructor wouldn't initialize it for the rest of the class

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Run() method of runnable function has no access to data members?

    Did you see that there is more than one variable named: board defined in the code? You assign a value to one of them. The other one's value stays at null. The local variable "shadows" the class level variable. A good IDE should report that with a warning because it is never a good idea to do that.
    Solution: remove the definition of board from inside the method and use the one at the class level.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    ineedahero (December 9th, 2013)

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Run() method of runnable function has no access to data members?

    Board board = new Board(); creates a new variable local to the constructor.
    Are you sure this is what you meant to do?

  7. #6
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Run() method of runnable function has no access to data members?

    Ah, stupid mistake.

    Thanks for the help!!!

  8. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Run() method of runnable function has no access to data members?

    Nothing is magic. You created a new "board" variable in your constructor, thus "hiding" the class board variable. So the board variable that you initialize in your constructor is not the same one that is used in your run method.

    EDIT: wow, everyone jumped all over this while I was typing, lol.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Runnable jar file wont run
    By bobscool123 in forum Object Oriented Programming
    Replies: 6
    Last Post: June 7th, 2013, 11:25 AM
  2. How to use data from obtainMessage in Runnable?
    By iceman999 in forum Android Development
    Replies: 0
    Last Post: February 8th, 2013, 05:23 AM
  3. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM
  4. Javascript function window.onload cant run
    By Stefan_Lam in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 11:33 AM