1

i have a javafx application and it has two classes in it, namely "Client" and "Interface_Client_Impl". i have defined an int array in "Client" class and a function that initializes that array. when i tried to access the array contents at index i from Interface_Client_Impl, it always returns 0. Interface_Client_Impl class is accessed remotely and im able to get the values of variables but not the array. where am i going wrong. -_- this is what i have.

public class Client extends Application
{
    public int size = 4;
    public int array[] = new int[size];
    public int min = 1;
    public int max = 99;

    public static void main(String[] args) throws Exception
    {
         launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws NotBoundException, RemoteException 
    {
        initialize_arr();
        //other codes
    }

    public void initialize_arr()
    {
        Random rand = new Random();
        for(int i = 0; i < size; i++)
        {//initialize with random values
            int val = rand.nextInt(max - min + 1) + min;
            array[i] = val;
        }
    }
}

//another class

public class Interface_Client_Impl extends UnicastRemoteObject implements Interface_Client
{
    public Client client = new Client();

    @Override
    public int exchange(int val)
    {
        Random rand = new Random();
        int pos = rand.nextInt(client.size);
        int return_val = client.array[pos];
        client.array[pos] = val;
        return return_val;
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Deo
  • 59
  • 6
  • 1
    `Interface_Client_Impl.client` instance is only created (constructor called), but not used by the fx framework, thus it's `start` method isn't triggered. Therefore, the array is not initialized. You have to access the instance created by framework, where the `start(Stage primaryStage)` has been called, and not to create your own. – fairtrax Sep 13 '17 at 07:36
  • The answer to [this question](https://stackoverflow.com/questions/33303167/javafx-can-application-class-be-the-controller-class) has a relevant explanation. – Itai Sep 13 '17 at 07:45

1 Answers1

0

You have just created the instance for Client in class Interface_Client_Impl

You need to invoke the array initialization of client instance. You can do via following two ways

By invoking public method client.start(primaryStage)

or

by invoking public method client.initialize_arr()

nagendra547
  • 5,672
  • 3
  • 29
  • 43