2016-08-11 7 views
0

学校の宿題のためにYouTubeでチュートリアルからim devlop javaFxログインアプリ。ログインボタンをクリックしてください。ログイン機能はウィンドウメインシーン(AdminLayout.fxml)を表示できません。ログインウィンドウを隠すだけです。ありがとうございことJavafxはログイン後にメインウィンドウfxmlを表示できません

これはLoginLayoutController.javaで

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) 
    ... 45 more 
Caused by: java.lang.NullPointerException 
    at mpn.manda.controller.LoginLayoutController.Login(LoginLayoutController.java:58) 
    ... 55 more 

私のエラー行58で

Pane p = fxmlLoader.load(getClass().getResource("view/AdminLayout.fxml").openStream()); 

ファイルLoginLayoutController.java

package mpn.manda.controller; 

import java.io.IOException; 
import java.net.URL; 
import java.sql.SQLException; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 

import javafx.fxml.Initializable; 
import mpn.manda.model.LoginModel; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.control.PasswordField; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 

/** 
* FXML Controller class 
* 
* @author Xaxxis 
*/ 
public class LoginLayoutController implements Initializable { 
    public LoginModel loginModel = new LoginModel(); 


    @FXML 
    private Label isConnected; 
    @FXML 
    private TextField usernameField; 
    @FXML 
    private PasswordField passwordField; 
    @Override 
    public void initialize(URL location, ResourceBundle resource) { 
     if (loginModel.isDbConnected()) { 
      isConnected.setText("Connected"); 
     } else { 
      isConnected.setText("Not Connected"); 
     } 
    } 

    public void Login (ActionEvent event) { 

     try { 
      if (loginModel.isLogin(usernameField.getText(), passwordField.getText())) { 

       ((Node)event.getSource()).getScene().getWindow().hide(); 
       Stage primaryStage = new Stage(); 
       FXMLLoader fxmlLoader = new FXMLLoader(); 
       Pane p = fxmlLoader.load(getClass().getResource("view/AdminLayout.fxml").openStream()); 
       AdminLayoutController adminController = (AdminLayoutController)fxmlLoader.getController(); 
       adminController.GetUser(usernameField.getText()); 
       Scene scene = new Scene(p); 
       scene.getStylesheets().add(getClass().getResource("css/application.css").toExternalForm()); 
       primaryStage.setScene(scene); 
       primaryStage.show();  

      } else { 
       isConnected.setText("Username and password is not correct"); 
      } 
      } catch (SQLException e) { 
       isConnected.setText("Username and password is not correct"); 
      } 
      catch(IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

MainApp.java

package mpn.manda; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

/** 
* 
* @author Xaxxis 
*/ 
public class MainApp extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     try { 
      Parent root = FXMLLoader.load(getClass().getResource("view/LoginLayout.fxml")); 
      Scene scene = new Scene(root); 
      scene.getStylesheets().add(getClass().getResource("css/application.css").toExternalForm()); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     launch(args); 
    }  
} 
</pre> 

AdminLayoutController.java 

<pre> 
package mpn.manda.controller; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Label; 

/** 
* FXML Controller class 
* 
* @author Xaxxis 
*/ 
public class AdminLayoutController implements Initializable { 

    @FXML 
    private Label userLabel; 
    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     // TODO 
    }  
    public void GetUser(String user) { 
     // TODO 
     userLabel.setText(user); 
    } 

    public void Logout(ActionEvent event) { 

} 
} 

LoginLayout.fxml

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

<?import java.net.URL?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.PasswordField?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.paint.RadialGradient?> 
<?import javafx.scene.paint.Stop?> 
<?import javafx.scene.text.Font?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mpn.manda.controller.LoginLayoutController"> 
    <stylesheets> 
     <URL value="@/mpn/manda/css/application.css" /> 
    </stylesheets> 
    <children> 
     <Label fx:id="isConnected" layoutX="18.0" layoutY="94.0" prefHeight="28.0" prefWidth="485.0" text="Status" textFill="#f20202"> 
     <font> 
      <Font size="15.0" /> 
     </font> 
     </Label> 
     <Label layoutX="94.0" layoutY="135.0" text="Username" /> 
     <Label layoutX="94.0" layoutY="183.0" text="Password" /> 
     <TextField fx:id="usernameField" layoutX="203.0" layoutY="130.0" promptText="Your username" /> 
     <PasswordField fx:id="passwordField" layoutX="203.0" layoutY="178.0" promptText="Your password" /> 
     <Button layoutX="203.0" layoutY="236.0" mnemonicParsing="false" onAction="#Login" prefHeight="27.0" prefWidth="78.0" text="Login..." /> 
     <Button layoutX="288.0" layoutY="236.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="78.0" text="Cancel" /> 
     <Label layoutX="47.0" layoutY="24.0" prefHeight="59.0" prefWidth="381.0" text="Welcom To The App"> 
     <font> 
      <Font size="31.0" /> 
     </font> 
     <textFill> 
      <RadialGradient centerX="0.5" centerY="0.5" radius="0.5"> 
       <stops> 
        <Stop color="#3c361c" /> 
        <Stop color="#9a8d8d" offset="0.9330855018587361" /> 
        <Stop color="#9a8d8d" offset="1.0" /> 
       </stops> 
      </RadialGradient> 
     </textFill> 
     </Label> 
    </children> 
</AnchorPane> 

AdminLayout.fxml

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.layout.BorderPane?> 

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mpn.manda.controller.AdminLayoutController"> 
    <top> 
     <Label fx:id="userLabel" prefHeight="17.0" prefWidth="154.0" text="Halo, " BorderPane.alignment="CENTER" /> 
    </top> 
    <bottom> 
     <Button mnemonicParsing="false" text="Logout.." BorderPane.alignment="CENTER" /> 
    </bottom> 
</BorderPane> 

答えて

1

パスが間違っています。

getClass().getResource("view/AdminLayout.fxml") 

は現在のクラスとの相対的なパスを解決します(したがって、/mpn/manda/controller/view/AdminLayout.fxmlを探しています)。 、あなたは

getClass().getResource("/mpn/manda/view/AdminLayout.fxml") 

は(クラスパスへの相対的な解決をリード/、注意して行うことができますいずれかviewパッケージはmpn.manda.viewです(リソースがMainAppで正しく動作するため)と仮定すると、(同じパッケージにすなわちAdminLayout.fxmlLoginLayout.fxmlです) )、または

MainApp.class.getResource("view/AdminLayout.fxml") 

MainAppクラスに関連するパスを解決します。

..を「親パッケージ」を参照するようにしないでください。ファイルシステムからリソースやクラスを読み込んでいるとき(開発中など)には動作しますが、アプリケーションがjarファイルにバンドルされていると失敗しますファイル(これは本番時になります)。


脇に、ストリームの代わりにFXMLリソースのURLを指定してFXMLを読み込むことをお勧めします。これは、FXMLLoaderへのURLを指定しないと、FXMLファイル自体のresource resolutionsが失敗するためです。だから私はあなたが1つのFXMLファイル用のパッケージ(view)とコントローラの1(controller)を持っている構造(Iは、ないFWIWを行う)、別の技術あなたを好きなら

FXMLLoader fxmlLoader = new FXMLLoader(MainApp.class.getResource("view/AdminLayout.fxml")); 
Pane p = fxmlLoader.load(); 
// ... 

をお勧めします好むかもしれビューのリソースを解決するために、空のクラスを定義することです:

package mpn.manda.view ; 

public abstract class View { } 

をそして今、あなたは

FXMLLoader loader = new FXMLLoader(View.class.getResource("LoginLayout.fxml")); 
Parent root = loader.load(); 
でどこからでも任意のFXMLを読み込むことができます

FXMLLoader loader = new FXMLLoader(View.class.getResource("AdminLayout.fxml")); 
Parent p = loader.load(); 

などなど

+0

イム非常に大きな感謝ジェームズあなたのアドバイスのために、最終的には自分のアプリケーションが実行されている:) – Xaxxis

関連する問題