从JavaFX控制器调用另一个类的方法?

时间:2017-02-12 03:40:44

标签: java methods javafx

我正在尝试从我的java控制器类中调用另一个类的方法,但是当我尝试单击loginbutton时,我似乎无法正常工作。 这是我想要做的 加载FXML>>输入登录名和密码>>单击LOGIN btn>>调用另一个类的方法来检查凭据 当我单击“登录”按钮

时,会遇到错误作为运行时

SECOND CLASS(我称之为方法的那个)

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) && password.equals(PASSWORD)) {
        Display_Text("Login Successful!");
    } else {
        Display_Text("Failed to Login. Incorrect Username and/or Password");
    }

}

}

JAVAFX FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.effect.ColorAdjust?>
<?import javafx.scene.effect.DropShadow?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<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">
   <children>
      <Text fill="WHITE" layoutX="139.0" layoutY="30.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome" textAlignment="CENTER" wrappingWidth="131.6708984375">
         <font>
            <Font size="28.0" />
         </font>
      </Text>
      <JFXTextField fx:id="username_text" accessibleText="username_text" alignment="CENTER" focusColor="WHITE" labelFloat="true" layoutX="120.0"     layoutY="78.0" promptText="Username" unFocusColor="#bdbdbd">
             <effect>
            <ColorAdjust />
     </effect></JFXTextField>
      <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" />
  <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" />
  <Label fx:id="loginresp_lab" alignment="CENTER" layoutX="76.0" layoutY="159.0" prefHeight="21.0" prefWidth="258.0" textAlignment="CENTER" textFill="#d32f2f">
     <font>
        <Font name="System Bold" size="15.0" />
     </font>
  </Label>
  <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">
         <image>
        <Image url="@xclose.png" />
     </image>
     <effect>
        <DropShadow blurType="GAUSSIAN" color="#000000af" />
     </effect>
  </ImageView>
   </children>
</AnchorPane>

JAVA CLASS

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);
}

}

JAVA CONTROLLER     包javafxapplication4;

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
}

}

错误

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

1 个答案:

答案 0 :(得分:1)

这似乎是一个简单的NPE(空指针异常)。我不会解析所有这些代码,但这一行:

username.equals(USERNAME) && password.equals(PASSWORD) 

表示usernamepassword为空。要注意无效检查,以及如何防范NPE。

这是解决实际问题的简单方法:

USERNAME.equals(username) && PASSWORD.equals(password)

PASSWORDUSERNAME是常量。这些永远不会为空。进行此更改后,您可以确切了解这些值为空的原因。

在进行String比较时,Apache StringUtils.equals是一种方便的方法,可以在比较之前进行空检查。