2017-02-12 13 views
0

私は、私のJavaコントローラクラスから別のクラスのメソッドを呼び出そうとしていますが、loginbuttonをクリックしようとするとエラーが出るので動作しません。 ここに私がやろうとしているのは FXMLを読み込む>>ログインとパスワードを入力>>ログインをクリックするbtn >>クレームを確認するために別のクラスを呼び出す ログインボタンをクリックするとランタイムとしてエラーが発生するJavaFXコントローラから別のクラスを呼び出す方法は?

第二のクラス(iはメソッドを呼び出すFRM)

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クラス

​​

}

JAVAコントローラ パッケージ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

こんにちは!スタックオーバーフローへようこそ。 [良い質問をする方法](http://stackoverflow.com/help/how-to-ask)をご覧ください。それが現れて、あなたは一塊のコードを投げ捨てて、あなたにはエラーがあると言いましたが、スタックトレースや文脈は与えられませんでした。 –

+0

以前よりも優れていますか? –

答えて

1

これは、単純なNPE(NULLポインタ例外)のように見えます。私はこのすべてのコードを解析しませんが、この行:

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

usernamepasswordのどちらかがNULLであることを示しています。ヌルチェックとNPEの防御方法を意識してください。

これは、実際の問題に取得する簡単な修正は次のとおりです。

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

PASSWORDUSERNAMEは定数です。これらはnullになることはありません。この変更を行った後、これらの値がnullである理由を正確に把握できます。

比較すると、は、比較の前にヌルチェックを行う便利な方法です。Stringを比較すると、便利な方法です。

関連する問題