61541
JavaFXを最近勉強し始めたのですが、コントローラークラスから別のJavaクラスを実行したいのですが、ボタンを押したところエラーを吐き出して実行できません。  
controllerクラスから別クラスのメソッドを実行するにはどうすればいいのでしょうか？

以下がコードです。

Mainクラス

```

    package application;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    
    public class Main extends Application{
    
        public static void main(String[] args) {
    
            System.out.println("Program Running...");
            launch(args);
            System.out.println("...Program End");
        }
    
        @Override
        public void start(Stage primaryStage) throws Exception {
    
            Stage stage = primaryStage;
    
            try {
                BorderPane root = (BorderPane)FXMLLoader.load(getClass().getResource("Application.fxml"));
                Scene scene = new Scene(root, 640, 400);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                stage.setScene(scene);
                stage.show();
    
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    
```

コントローラークラス

```

    package application;
    
    import application.process.ProcessOrder;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.scene.control.Button;
    import javafx.scene.control.TextField;
    import javafx.scene.input.InputEvent;;
    
    public class Controller {
    
        String codePath;
        ProcessOrder po;
    
        @FXML
        private Button dfgButton;
        @FXML
        private TextField filePath;
    
        @FXML
        public void buttonClicked(ActionEvent e) {
            System.out.println("getPathButton clicked...");
            this.codePath = filePath.getText().toString();
            System.out.println(this.codePath);
            po.testP();
        }
    
```

実行したいメソッドがあるクラス

```

    package application.process;
    
    public class ProcessOrder {
    
        public void testP() {
    
            System.out.println("testP run");
        }
    }
    
```

fxml

```

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Menu?>
    <?import javafx.scene.control.MenuBar?>
    <?import javafx.scene.control.MenuItem?>
    <?import javafx.scene.control.SeparatorMenuItem?>
    <?import javafx.scene.control.TextField?>
    <?import javafx.scene.image.Image?>
    <?import javafx.scene.image.ImageView?>
    <?import javafx.scene.layout.BorderPane?>
    <?import javafx.scene.layout.HBox?>
    <?import javafx.scene.layout.Region?>
    
    <BorderPane prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
       <top>
          <MenuBar BorderPane.alignment="CENTER">
            <menus>
              <Menu mnemonicParsing="false" text="File">
                <items>
                      <MenuItem mnemonicParsing="false" text="Open..." />
                      <SeparatorMenuItem mnemonicParsing="false" />
                  <MenuItem mnemonicParsing="false" text="Close" />
                </items>
              </Menu>
              <Menu mnemonicParsing="false" text="Help">
                <items>
                  <MenuItem mnemonicParsing="false" text="About" />
                </items>
              </Menu>
            </menus>
          </MenuBar>
       </top>
       <center>
          <ImageView fitHeight="190.0" pickOnBounds="true" preserveRatio="true" BorderPane.alignment="CENTER">
             <BorderPane.margin>
                <Insets bottom="16.0" left="16.0" right="16.0" top="16.0" />
             </BorderPane.margin>
             <image>
                <Image url="@../image/DFG.png" />
             </image>
          </ImageView>
       </center>
       <bottom>
          <HBox minHeight="0.0" minWidth="0.0" BorderPane.alignment="CENTER">
             <children>
                <Region prefWidth="100.0">
                   <HBox.margin>
                      <Insets bottom="8.0" left="8.0" right="8.0" top="8.0" />
                   </HBox.margin>
                </Region>
                <TextField fx:id="filePath" onAction="#codePath" prefWidth="300.0">
                   <HBox.margin>
                      <Insets />
                   </HBox.margin>
                   <padding>
                      <Insets top="8.0" />
                   </padding>
                </TextField>
                <Region layoutX="18.0" layoutY="18.0" prefWidth="50.0">
                   <HBox.margin>
                      <Insets top="8.0" />
                   </HBox.margin>
                </Region>
                <Button fx:id="dfgButton" onAction="#buttonClicked" text="Change to dfg..." />
             </children>
             <BorderPane.margin>
                <Insets bottom="40.0" left="8.0" right="8.0" />
             </BorderPane.margin>
          </HBox>
       </bottom>
    </BorderPane>
    
```

エラーメッセージ

```

    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
        at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8411)
        at javafx.scene.control.Button.fire(Button.java:185)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
        at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
        at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
        at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
        at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$358(GlassViewEventHandler.java:432)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
        at com.sun.glass.ui.View.notifyMouse(View.java:937)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
        at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
        ... 48 more
    Caused by: java.lang.NullPointerException
        at application.Controller.buttonClicked(Controller.java:25)
        ... 58 more
    
```
CC BY-SA 4.0
2019-12-18T06:55:23.217
61538
2019-12-18T07:36:21.253
31842
question
java java8 javafx
JavaFXでコントーローラーから別クラスを実行したい
初期値を指定しないで変数宣言したので、フィールド変数`po`はnullです。

```

    ProcessOrder po;
    
```

その後、値が代入されることなく、nullのままの`po`のメソッドにアクセスしようとしたので`NullPointerException`がスローされます。

```

    po.testP();
    
```
CC BY-SA 4.0
2019-12-18T07:36:21.253
61541
2019-12-18T07:36:21.253
21092
61538
answer