2016-09-02 1 views
2

das ist meine erste Frage, ich fühle mich wie ich würde das ganze Web durchsuchen, aber irgendwie gibt es keine Antwort auf meine Frage. Vielleicht habe ich ein einfaches Problem - ziemlich neu in Java. Im folgenden Code sieht alles gut für mich aus. Aber wenn ich managertree.setRoot(rootItem) ausführen, erhalte ich eine Ausnahme.JavaFX TreeView setRoot führt zu Exception

Hoffe jemand könnte mich eine Minute verbringen und es mir ansehen.

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); 
    } 

}

Die 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> 

Und der Compiler wirft:

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

diesen Code setzen 'managertree.setShowRoot (false); '' after stage.show() 'oder nach' setRoot() 'und sehen, ob der Fehler verschwindet – Elltz

Antwort

1

Das eigentliche Problem hier könnte ein NullPointer exception Ursache fxml Artikel sein (managertree [was sollte geschrieben werden als managerTree]) wurden noch nicht initialisiert.

Machen Sie Ihre Klasse implementieren Initializable Schnittstelle und @override es ist Methode.Sobald diese Methode aufgerufen werden Sie sind sorry, dass alle fxml Elemente nicht mehr null sind.

ändern Sie dann Ihre fxml Elemente dort, Code:

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 Editiert die an swer.Code bereitgestellt. – GOXR3PLUS

1

Vielen Dank für Ihre Beratung, die den Trick. Aus irgendeinem Grund wird das inititializable nicht in Main ausgeführt, ich würde einen Controller hinzufügen. Hier ist die finaly Arbeitsbeispiel:

des Controllers definieren:

<?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> 

Die Controller-Klasse:

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); 
    } 

} 

und die Hauptklasse:

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); 
    }  

} 
Verwandte Themen