title: Calling method of another class from JavaFX controller?
body: <p>I am trying to call a method from another class from my java controller class, but that doesnt seem to work as i get the error when i try to click the loginbutton.
  Here is what i am trying to do 
  Load FXML>>Enter Login and Password >> Click LOGIN btn>>Call a method frm another class to check credentials 
  The Error is encountered as a Runtime, when I Click the Login Button</p>
  
  <p>SECOND CLASS (frm where i call the method)</p>
  
  <pre><code>package javafxapplication4;
  
  import static globalmethods.GlobalMethods.Display_Text;
  
  public class CheckLogin{
  
  public static final String USERNAME = "admin";//Store the credentials in     a file
  public static final String PASSWORD = "admin";//then retrive them here
  String username, password;
  
  static void startCheck() {
      CheckLogin obj = new CheckLogin();
      obj.getVars();
      obj.checkLoginCredentials();
      }
  
      public void getVars() {
          LoginController obj1 = new LoginController();
          username = obj1.getUsername_i();
      password = obj1.getPassword_i();
  
  }
  
  public void checkLoginCredentials() {
      if (username.equals(USERNAME) &amp;&amp; password.equals(PASSWORD)) {
          Display_Text("Login Successful!");
      } else {
          Display_Text("Failed to Login. Incorrect Username and/or Password");
      }
  
  }
  
  }
  </code></pre>
  
  <p>JAVAFX FXML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;?import com.jfoenix.controls.JFXButton?&gt;
  &lt;?import com.jfoenix.controls.JFXTextField?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  &lt;?import javafx.scene.effect.ColorAdjust?&gt;
  &lt;?import javafx.scene.effect.DropShadow?&gt;
  &lt;?import javafx.scene.image.Image?&gt;
  &lt;?import javafx.scene.image.ImageView?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.text.Font?&gt;
  &lt;?import javafx.scene.text.Text?&gt;
  
  &lt;AnchorPane id="AnchorPane" prefHeight="253.0" prefWidth="417.0" style="-    fx-background-color: #03A9F4;" xmlns="http://javafx.com/javafx/8.0.111"     xmlns:fx="http://javafx.com/fxml/1"     fx:controller="javafxapplication4.LoginController"&gt;
     &lt;children&gt;
        &lt;Text fill="WHITE" layoutX="139.0" layoutY="30.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome" textAlignment="CENTER" wrappingWidth="131.6708984375"&gt;
           &lt;font&gt;
              &lt;Font size="28.0" /&gt;
           &lt;/font&gt;
        &lt;/Text&gt;
        &lt;JFXTextField fx:id="username_text" accessibleText="username_text" alignment="CENTER" focusColor="WHITE" labelFloat="true" layoutX="120.0"     layoutY="78.0" promptText="Username" unFocusColor="#bdbdbd"&gt;
               &lt;effect&gt;
              &lt;ColorAdjust /&gt;
       &lt;/effect&gt;&lt;/JFXTextField&gt;
        &lt;JFXTextField fx:id="password_text" accessibleText="password_text" alignment="CENTER" focusColor="WHITE" labelFloat="true" layoutX="121.0" layoutY="127.0" prefHeight="21.0" prefWidth="169.0" promptText="Password" unFocusColor="#bdbdbd" /&gt;
    &lt;JFXButton fx:id="login_btn" buttonType="RAISED" layoutX="165.0" layoutY="199.0" onAction="#OnLoginClicked" prefHeight="40.0" prefWidth="87.0" style="-fx-background-color: #FFC107;" text="Login" /&gt;
    &lt;Label fx:id="loginresp_lab" alignment="CENTER" layoutX="76.0" layoutY="159.0" prefHeight="21.0" prefWidth="258.0" textAlignment="CENTER" textFill="#d32f2f"&gt;
       &lt;font&gt;
          &lt;Font name="System Bold" size="15.0" /&gt;
       &lt;/font&gt;
    &lt;/Label&gt;
    &lt;ImageView fx:id="close_btn" fitHeight="29.0" fitWidth="30.0"     focusTraversable="true" layoutX="388.0" layoutY="4.0"     onMouseClicked="#OnExitClicked" pickOnBounds="true" preserveRatio="true"     smooth="false"&gt;
           &lt;image&gt;
          &lt;Image url="@xclose.png" /&gt;
       &lt;/image&gt;
       &lt;effect&gt;
          &lt;DropShadow blurType="GAUSSIAN" color="#000000af" /&gt;
       &lt;/effect&gt;
    &lt;/ImageView&gt;
     &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>
  
  <p>JAVA CLASS</p>
  
  <pre><code>package javafxapplication4;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.stage.Stage;
  import javafx.stage.StageStyle;
  
  public class GUITest2 extends Application {
  
  @Override
  public void start(Stage stage) throws Exception {
      Parent root =             FXMLLoader.load(getClass().getResource("LoginWindow.fxml"));
  
      Scene scene = new Scene(root);
      stage.setResizable(false);
      stage.initStyle(StageStyle.UNDECORATED);
      stage.setScene(scene);
      stage.show();
  }
  
  
  public static void main(String[] args) {
      launch(args);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>JAVA CONTROLLER 
      package javafxapplication4;</p>
  
  <pre><code>import com.jfoenix.controls.JFXButton;
  import com.jfoenix.controls.JFXTextField;
  import static globalmethods.GlobalMethods.Display_Text;
  import java.io.IOException;
  import java.net.URL;
  import java.util.ResourceBundle;
  import javafx.event.ActionEvent;
  import javafx.event.Event;
  import javafx.fxml.FXML;
  import javafx.fxml.FXMLLoader;
  import javafx.fxml.Initializable;
  import javafx.fxml.JavaFXBuilderFactory;
  import javafx.scene.Node;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Label;
  import javafx.scene.image.ImageView;
  import javafx.scene.input.MouseEvent;
  import javafx.stage.Stage;
  import static javafxapplication4.CheckLogin.startCheck;
  
  public class LoginController implements Initializable {
  
  CheckLogin obj = new CheckLogin();
  
  private String Username_i, Password_i;
  @FXML
  private JFXTextField username_text;
  
  @FXML
  private JFXTextField password_text;
  
  @FXML
  private JFXButton login_btn;
  
  @FXML
  private Label loginresp_lab;
  
  @FXML
  void OnLoginClicked(ActionEvent event) {
      startCheck();
  
  }
  
  public String getUsername_i() {
      return Username_i;
  }
  
  public String getPassword_i() {
      return Password_i;
  }
  
  @FXML
  private ImageView close_btn;
  
  @FXML
  void OnExitClicked(MouseEvent event) {
      Display_Text("Exit!");
      System.exit(0);
  }
  
  @Override
  public void initialize(URL url, ResourceBundle rb) {
      // TODO
  }
  
  }
  </code></pre>
  
  <p>ERROR </p>
  
  <pre><code>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:8413)
  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:381)
  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$354(GlassViewEventHandler.java:417)
  at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
  at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
  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$148(WinApplication.java:191)
  at java.lang.Thread.run(Thread.java:745)
    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 javafxapplication4.CheckLogin.checkLoginCredentials(CheckLogin.java:25)
          at javafxapplication4.CheckLogin.startCheck(CheckLogin.java:14)
      at    javafxapplication4.LoginController.OnLoginClicked(LoginController.java:52)
      ... 58 more
  </code></pre>