2017-02-20 25 views
0

ボタンを押したときにレイアウトを切り替えようとしています。私は多くのことを試して、ここでstackoverflowを検索しましたが、答えは私のために応募していないようです。JavaFX FXMLシーン切り替えが機能していません[NullPointerException]

マイFrontendView.class:ライン62で

/* FrontendView Class by @Aebian */ 
package org.aebian.umFrontend.view; 

import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.image.Image; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

import java.io.IOException; 


public class FrontendView extends Application { 

    @FXML 
    protected static Button btnLogin, btnSave, btnDiscard, btnRefresh, btnEdit; 
    @FXML 
    private Stage primaryStage; 
    @FXML 
    private Scene scene; 
    @FXML 
    private VBox vDefault; 
    @FXML 
    protected BorderPane rootLayout; 

    private double xOffset = 0; 
    private double yOffset = 0; 

    @Override 
    public void start(Stage primaryStage) { 

     GridPane root = new GridPane(); 
     primaryStage.setMinWidth(800); 
     primaryStage.initStyle(StageStyle.UNDECORATED); 

     primaryStage.setMinHeight(600); 
     primaryStage.setResizable(false); 
     primaryStage.getIcons().add(new Image("file:res/images/uMgmt.png")); 

     primaryStage.setTitle("User Management"); 
     Scene scene = new Scene(root, 800, 600); 

     primaryStage.setScene(scene); 
     this.primaryStage = primaryStage; 
     root.setAlignment(Pos.CENTER); 

     showRoot(); 
     showLogin(); 
    } 

    public void showRoot() { // Load root layout 
     try { 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(FrontendView.class.getResource("UI/umFrontendRoot.fxml")); 
      rootLayout = loader.load(); 
      rootLayout.setId("umFrontend"); 

      //Let the root Layout be able to moved around. 
      rootLayout.getTop().setOnMousePressed(new EventHandler<MouseEvent>() { 
       @Override 
       public void handle(MouseEvent event) { 
        xOffset = event.getSceneX(); 
        yOffset = event.getSceneY(); 
       } 
      }); 
      rootLayout.getTop().setOnMouseDragged(new EventHandler<MouseEvent>() { 
       @Override 
       public void handle(MouseEvent event) { 
        primaryStage.setX(event.getScreenX() - xOffset); 
        primaryStage.setY(event.getScreenY() - yOffset); 
       } 
      }); 

      // Show the scene containing the root layout. 
      Scene scene = new Scene(rootLayout); 
      primaryStage.setScene(scene); 
      String css = FrontendView.class.getResource("UI/umFrontendStyles.css").toExternalForm(); 
      scene.getStylesheets().clear(); 
      scene.getStylesheets().add(css); 

      primaryStage.show(); 

     } catch (IOException e) { 
     } 
    } 

    public void showLogin() { // Load the login page. 
     try { 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(FrontendView.class.getResource("UI/umFrontendLogin.fxml")); 
      vDefault = loader.load(); 
      // Set login to center of root layout. 
      rootLayout.setCenter(vDefault); 
     } catch (IOException e) { 
     } 
    } 

    public void showAbout() { // Load the about page. 
     try { 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(FrontendView.class.getResource("UI/umFrontendAbout.fxml")); 
      vDefault = loader.load(); 

      // Set about page to center of root layout. 
      this.rootLayout.setCenter(vDefault); 
     } catch (IOException e) { 
     } 
    } 

    public void showAdminOverview() { // Load the admin overview/edit page. 
     try { 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(FrontendView.class.getResource("UI/umFrontendAdminOverview.fxml")); 
      VBox showAdminOverview = loader.load(); 
      // Set admin overview page to center of root layout. 
      rootLayout.setCenter(showAdminOverview); 
     } catch (IOException e) { 
     } 
    } 

    public void showUserOverview() { // Load the user overview/edit page. 
     try { 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(FrontendView.class.getResource("UI/umFrontendUserOverview.fxml")); 
      VBox showUserOverview = loader.load(); 
      // Set admin overview page to center of root layout. 
      rootLayout.setCenter(showUserOverview); 
     } catch (IOException e) { 
     } 
    } 

    /** 
    * Getter and setter. 
    * 
    * @return 
    */ 
    public BorderPane getBorderPane() { 
     return rootLayout; 
    } 

} 

私は正常に動作しているshowLogin()を呼び出します。私がボタンを介してこれを実行しようとすると失敗します。同じことがに当てはまります。 6234行目のshowLogin()をこれで置き換えるとうまくいきます。ボタンの上には表示されません。

マイFrontendViewController.class:

package org.aebian.umFrontend.view; 

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.scene.Parent; 
import javafx.scene.control.Alert; 
import javafx.scene.control.Button; 
import javafx.scene.control.ButtonType; 
import javafx.scene.control.MenuItem; 

import java.io.IOException; 
import java.util.Optional; 

public class FrontendViewController { 

    FrontendView FView = new FrontendView(); 
    Boolean admin = false; 

    @FXML 
    MenuItem appClose; 
    @FXML 
    MenuItem appAbout; 
    @FXML 
    Button buttonLogin; 
    @FXML 
    Parent root; 

    /* Button Events */ 

    @FXML 
    private void handleButtonAction(ActionEvent e) throws IOException { 

     if (e.getSource() == appClose) { // Exit application when user clicks on Edit > Close 
      System.exit(0); 
     } 

     else if (e.getSource() == appAbout) { // Show About page when user clicks on Help > About 
      FView.showAbout(); 
     } 

     else if (e.getSource() == buttonLogin) { // Function that gets triggered when the user presses the Login Button 
      if (admin == true) { 
       FView.showAdminOverview(); 
      } else { 
       FView.showUserOverview(); 
      } 
     } 

    } 

    /* Frontend Dialogs */ 

    public void delUserConfirmDialog() { 

     Alert alert = new Alert(Alert.AlertType.CONFIRMATION); 
     alert.setTitle("Delete User Dialog"); 
     alert.setHeaderText("Delete User $ss"); 
     alert.setContentText("Are you sure you want to delete the selected action?"); 

     Optional<ButtonType> result = alert.showAndWait(); 
     if (result.get() == ButtonType.OK) { 
      FView.showAdminOverview(); 
     } else { 
      // ... user chose CANCEL or closed the dialog 
     } 

    } 
} 

私はまた、動的にコントローラを設定しようとしましたが、それは何も変わっていませんでした。 私は、コンパイラからもらったエラーはここに、このいずれかです。私は、メソッドを実行することができますように

"C:\Program Files\Java\jdk1.8.0_121\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:6660,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;C:\Users\agoebbel\Tempo Box\Development & PS\Eclipse-Workspaces\userMan\out\production\userMan;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.3.4\lib\idea_rt.jar" org.aebian.umFrontend.Frontend 
Connected to the target VM, address: '127.0.0.1:6660', transport: 'socket' 
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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
    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.control.MenuItem.fire(MenuItem.java:462) 
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405) 
    at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358) 
    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.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) 
    ... 43 more 
Caused by: java.lang.NullPointerException 
    at org.aebian.umFrontend.view.FrontendView.showAbout(FrontendView.java:115) 
    at org.aebian.umFrontend.view.FrontendViewController.handleButtonAction(FrontendViewController.java:39) 
    ... 53 more 

showLogin()、showAbout()ので、1直接そのない入力ストリームのエラー。 あなたは私のFXMLファイルのいずれかが必要な場合:

umFrontendAbout.fxml:私はこの問題を解決するにはどうすればよい

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

<?import javafx.scene.paint.*?> 
<?import javafx.scene.text.*?> 
<?import java.lang.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.VBox?> 
<?import javafx.scene.paint.Color?> 
<?import javafx.scene.text.Font?> 

<VBox alignment="CENTER" prefHeight="600.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.aebian.umFrontend.view.FrontendView"> 
    <children> 
     <SplitPane focusTraversable="true" orientation="VERTICAL" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS"> 
      <items> 
       <AnchorPane prefHeight="576.0" prefWidth="328.0"> 
       <children> 
        <Label layoutX="272.0" layoutY="121.0" text="Java User Managment"> 
        <font> 
         <Font size="24.0" /> 
        </font> 
        </Label> 
        <Label layoutX="228.0" layoutY="164.0" text="A java written user management solution with mysql support" /> 
        <Label layoutX="251.0" layoutY="181.0" text="© 2017 by Adrian (Simmarith) &amp; Alexander (Aebian)" /> 
        <Label alignment="CENTER" layoutX="38.0" layoutY="14.0" minWidth="60.0" prefWidth="-1.0" style="&#10;" text="User Management \ About" textAlignment="CENTER" wrapText="false"> 
        <font> 
         <Font size="18.0" fx:id="x1" /> 
        </font> 
        <textFill> 
         <Color blue="0.624" green="0.624" red="0.624" fx:id="x2" /> 
        </textFill> 
        </Label> 
        <Label layoutX="338.0" layoutY="220.0" text="[email protected]" /> 
       </children> 
       </AnchorPane> 
      </items> 
     </SplitPane> 
    </children> 
</VBox> 

?私のために働いていない、私が試した

もの:

  • セットコントローラdynamiclly

((コントローラ)loader.getController()).setPrimaryStage(primaryStage)を介して、

  • は、コントローラ
+0

'FrontendViewController'で作成した' FrontendView'インスタンスで 'rootLayout'がなぜ初期化されると思いますか? 'start'メソッドによって(効果的に)初期化されます。このメソッドは、アプリケーションの起動時に作成されたインスタンスで呼び出されます。これは間違って構造化されているだけです。自分で 'Application'クラスのインスタンスを作成するべきではありません。また、' Application'クラスはアプリケーションを起動する以外何もしないでください。 FXアプリケーションのライフサイクルに合うようにコードを再構成する必要があります。 –

答えて

0

内setPrimaryStageメソッドを作成します)(メソッドのhandleButtonActionを呼び出す前にrootLayoutを初期化することがありますか?

私はあなたがshowRoot()にrootLayoutを読み込むことができますが、アプリケーションがメソッドhandleButtonAction()に到達すると、rootLayoutは初期化されません。

showLoot()で初期化したrootLayoutを保存して、handleButtonAction()のshowAboutで使用できるようにしてください。

+0

私はそれをどのように正確にしますか?私はshowRoot()を試しました。メソッドをthis.rootLayout = rootLayout;そのためのゲッターを作成しようとしました。 – Aebian

+0

申し訳ありませんがコードをよく読んでいませんでした。 FrontendViewController.classで 'BorderPane bPane = FView.getBorderPane();' 〜 'BorderPane rootLayout = FView.getBorderPane();の名前を変更し、showAbout()メソッドを ' this.rootLayoutで編集しようとしましたか? setCenter(vDefault); ' –

+0

私はそれに応じてコードを更新しました。また、スタックトレースも同じままです。うーん、私の推測では、showAbout()はshowRoot()の前にロードされていますが、それが真であれば正確にはわかりません。 – Aebian

0

私は、デバッグと試行後に問題を自分で修正しました。 問題は、コントローラが初期化された値を取得していないことです。メソッドの開始が実行されたときだけ存在します。したがって、私はコントローラ内に "Application"を持っている必要があります。または、JavaFXが既にMVCベースであるため、コントローラをViewクラス自体とマージできます。

私は2番目の選択肢とその作業を行った。 助けてくれてありがとう。

Aebian out。

関連する問題