2017-02-12 66 views
0

我想從我的java控制器類中的另一個類調用一個方法,但那似乎不起作用,因爲當我嘗試單擊loginbutton時出現錯誤。 這是我正在嘗試做的 加載FXML >>輸入登錄名和密碼>>點擊登錄btn >>調用另一個類的frm方法來檢查憑證 當我單擊登錄按鈕時遇到錯誤作爲運行系統從JavaFX控制器調用另一個類的方法?

第二類(FRM其中i調用方法)

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

​​

}

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 
} 

} 

ERROR

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 
+0

您好!歡迎來到Stack Overflow。請回顧[如何提出一個好問題](http://stackoverflow.com/help/how-to-ask)。就目前而言,你已經拋棄了一堆代碼,並說你有一個錯誤,但沒有堆棧跟蹤或上下文。 –

+0

比以前好? –

回答

1

這似乎是一個簡單的NPE(空指針異常)。我將無法通過這些代碼解析,但此行:

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

表明,無論是usernamepassword爲空。注意無效檢查,以及如何防範NPE。

這是一個簡單的辦法來獲得實際的問題:

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

PASSWORDUSERNAME是常數。這些不能爲空。進行此更改後,您可以精確搜索爲什麼這些值爲空。

在做String比較時,Apache StringUtils.equals是一種方便的方法,在比較之前進行空檢查。

相關問題