0

Here's the code :

Main File :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package testobjectarray;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 *
 * 
 */



public class TestObjectArray extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * The main() method is ignored in correctly deployed JavaFX application. main() serves only as fallback in case the application can not be launched through deployment artifacts, e.g., in IDEs
     * with limited FX support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);

        myClass[] c = new myClass[5];
        c[2].myMethod();
    }

}

Class declared outside :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package testobjectarray;

/**
 *
 * 
 */
public class myClass {
    public void myMethod(){
        System.out.println("Inside myMethod");
    }
}

Problem, I get errors when I compile i.e. initializing an array of objects and invoking the method. If I have just one object, it works great. Any help appreciated.

3
  • what are you trying to achieve by making an array of myClass just after launch(args) ? Commented May 24, 2014 at 0:00
  • @ItachiUchiha, I need to calculate distances for a collection of points. I just put a template here. And because I want to use it in JavaFX. Commented May 24, 2014 at 0:14
  • Trying to put it somewhere else, as the code below launch will never be called unless you close the stage Commented May 24, 2014 at 0:15

1 Answer 1

2

The problem with your code is you are initialising an array of size 5, but you never add values to it. Try the following code

myClass[] c = new myClass[5];
c[0] = new myClass();
c[1] = new myClass(); // and so on till c[4]
//now you can call the methods
c[1].myMethod();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.