2016-09-02 12 views
2

これは私の最初の質問です。私はウェブ全体を検索したいと思っていますが、どうにかして私の質問に答えはありません。たぶん私は単純な問題を抱えています.Javaにとってはまったく新しいものです。 次のコードでは、すべて私によく見えます。しかし、私がmanagertree.setRoot(rootItem)を実行すると、例外が発生します。JavaFX TreeView setRootの結果が例外

誰かが私に分を費やして見てもらいたいと思っています。

import java.io.IOException; 
import javafx.application.Application; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.TreeItem; 
import javafx.scene.control.TreeView; 
import javafx.stage.Stage; 

public class MainApp extends Application { 

    @FXML 
    private TreeView<String> managertree; 

    @Override 
    public void start(Stage stage) throws IOException {  
     Parent root = FXMLLoader.load(getClass().getResource("../view/MainLayout.fxml")); 
     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.setTitle("jLog");  

     TreeItem<String> rootItem = new TreeItem<>("Manager"); 
     rootItem.setExpanded(true); 
     for(int i = 1; i < 6;i++){ 
      TreeItem<String> item = new TreeItem<String>("Managing Logs " + i); 
      rootItem.getChildren().add(item); 
     } 
     managertree.setRoot(rootItem); 
     managertree.setShowRoot(false); 

     stage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

}

FXML:

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

<?import java.lang.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.StackPane?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2"> 
    <children> 
    <AnchorPane id="AnchorPane" layoutX="0.0" layoutY="0.0" prefHeight="36.0"> 
     <children> 
     <MenuBar prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
      <menus> 
      <Menu mnemonicParsing="false" text="File"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="Close" /> 
       </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="Delete" /> 
       </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Help"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="About" /> 
       </items> 
      </Menu> 
      </menus> 
     </MenuBar> 
     </children> 
    </AnchorPane> 
    <AnchorPane prefHeight="364.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="36.0" /> 
    <SplitPane dividerPositions="0.2826086956521739" focusTraversable="true" prefHeight="364.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="36.0"> 
     <items> 
     <AnchorPane id="manager" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0"> 
      <children> 
      <TreeView id="managertree" fx:id="managertree" editable="true" prefHeight="362.0" prefWidth="114.0" showRoot="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
      </children> 
     </AnchorPane> 
     <AnchorPane fx:id="rawlog" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" /> 
     </items> 
    </SplitPane> 
    </children> 
</AnchorPane> 

そして、コンパイラはスロー:

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 
at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.NullPointerException 
at jlog.main.MainApp.start(MainApp.java:32) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) 
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
... 1 more 
Exception running application jlog.main.MainApp 
+0

このコード 'managertree.setShowRootを置きます(false); '' after stage.show() 'または' 'setRoot()'の後にエラーがなくなるかどうかを確認する – Elltz

答えて

1

をここでの実際の問題はNullPointer exception原因FXMLアイテムかもしれません(managertree [すべきmanagerTree])はまだ初期化されていません。

クラスにInitializableインターフェイスを実装し、@overrideメソッドです。このメソッドが呼び出されるとすぐに、すべてのfxmlアイテムがnullにならなくなります。

すると、そこにコードをあなたのFXMLのアイテムを変更:トリックを行うあなたのアドバイスのための

import java.io.IOException; 
import java.net.URL; 
import java.util.ResourceBundle; 

import javafx.application.Application; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.TreeItem; 
import javafx.scene.control.TreeView; 
import javafx.stage.Stage; 

public class Main extends Application implements Initializable { 

    @FXML 
    private TreeView<String> managertree; 

    TreeItem<String> rootItem = new TreeItem<>("Manager"); 

    @Override 
    public void start(Stage stage) throws IOException { 

     //Load FXML file 
     Parent root = FXMLLoader.load(getClass().getResource("MainLayout.fxml")); 

     //... 
     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.setTitle("jLog"); 
     rootItem.setExpanded(true); 
     for (int i = 1; i < 6; i++) { 
      TreeItem<String> item = new TreeItem<String>("Managing Logs " + i); 
      rootItem.getChildren().add(item); 
     } 

     //Show the Stage 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     System.out.println("Main Controller initialized....."); 

     managertree.setRoot(rootItem); 
     managertree.setShowRoot(false); 

    } 

} 
+0

@Sudo swer.Codeが提供されました。 – GOXR3PLUS

1

おかげで、。何らかの理由で初期化がMainで実行されない場合は、コントローラを追加します。ここでfinaly作業例:

はコントローラを定義します。

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

<?import java.lang.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.layout.StackPane?> 

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="jlog.controllers.Main"> 
    <children> 
    <AnchorPane id="AnchorPane" layoutX="0.0" layoutY="0.0" prefHeight="36.0"> 
     <children> 
     <MenuBar prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
      <menus> 
      <Menu mnemonicParsing="false" text="File"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="Close" /> 
       </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="Delete" /> 
       </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Help"> 
       <items> 
       <MenuItem mnemonicParsing="false" text="About" /> 
       </items> 
      </Menu> 
      </menus> 
     </MenuBar> 
     </children> 
    </AnchorPane> 
    <AnchorPane prefHeight="364.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="36.0" /> 
    <SplitPane dividerPositions="0.2826086956521739" focusTraversable="true" prefHeight="364.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="36.0"> 
     <items> 
     <AnchorPane id="manager" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0"> 
      <children> 
      <TreeView id="managertree" fx:id="managerTree" disable="false" editable="true" prefHeight="362.0" prefWidth="114.0" showRoot="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
      </children> 
     </AnchorPane> 
     <AnchorPane fx:id="rawlog" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" /> 
     </items> 
    </SplitPane> 
    </children> 
</AnchorPane> 

[コントローラクラス:

package jlog.controllers; 

import java.net.URL; 
import java.util.ResourceBundle; 

import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TreeItem; 
import javafx.scene.control.TreeView; 

public class Main implements Initializable { 

    @FXML 
    private TreeView<String> managerTree; 

    TreeItem<String> rootItem = new TreeItem<>("Manager"); 

    public void initialize(URL arg0, ResourceBundle arg1) { 
     rootItem.setExpanded(true); 
     for(int i = 1; i < 6;i++){ 
      TreeItem<String> item = new TreeItem<String>("Managing Logs " + i); 
      rootItem.getChildren().add(item); 
     } 
     managerTree.setRoot(rootItem); 
     managerTree.setShowRoot(false); 
    } 

} 

、メインクラス:

package jlog.main; 

import java.io.IOException; 
import java.net.URL; 
import java.util.ResourceBundle; 

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

public class Main extends Application { 

    @Override 
    public void start(Stage stage) throws IOException {  

     Parent root = FXMLLoader.load(getClass().getResource("../view/Main.fxml")); 

     Scene scene = new Scene(root); 
     stage.setScene(scene); 
     stage.setTitle("jLog");  

     stage.show();  
    } 

    public static void main(String[] args) { 
     launch(args); 
    }  

} 
関連する問題