2015-10-20 77 views
0

我写一个包含ListView JavaFX应用程序。我尝试在初始化时填充列表视图并单击按钮会引发异常,并且由于我对javaFx的新手,我无法找到解决方案。请帮我找出原因,谢谢。填充的JavaFX的ListView抛出异常

FXML documnet

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

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

<BorderPane fx:id="root_borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="405.0" prefWidth="720.0" stylesheets="@styles/style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="managefilesclientnetbeans.FXMLDocumentController"> 
    <left> 
     <VBox BorderPane.alignment="CENTER"> 
      <children> 
       <GridPane> 
        <columnConstraints> 
         <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
         <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
        </columnConstraints> 
        <rowConstraints> 
         <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
        </rowConstraints> 
        <children> 
         <Label fx:id="listLabel" text="Available files" GridPane.halignment="LEFT" /> 
         <Button fx:id="refreshButton" alignment="CENTER_RIGHT" mnemonicParsing="false" onMouseClicked="#onRefreshButtonClicked" text="Button" GridPane.columnIndex="1" GridPane.halignment="RIGHT" /> 
        </children> 
        <padding> 
         <Insets bottom="2.0" left="2.0" right="2.0" top="2.0" /> 
        </padding> 
       </GridPane> 
       <ListView prefHeight="286.0" prefWidth="166.0" VBox.vgrow="ALWAYS" /> 
      </children> 
     </VBox> 
    </left> 
    <center> 
     <GridPane> 
      <columnConstraints> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" /> 
      </columnConstraints> 
      <rowConstraints> 
       <RowConstraints minHeight="10.0" vgrow="SOMETIMES" /> 
       <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
      </rowConstraints> 
      <children> 
       <Pane fx:id="previewPane" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" GridPane.vgrow="ALWAYS" /> 
       <HBox fx:id="controlsPane" alignment="CENTER_LEFT" spacing="10.0" GridPane.rowIndex="1"> 
        <children> 
         <Button fx:id="downloadButton" mnemonicParsing="false" text="Download" /> 
         <Button fx:id="uploadButton" mnemonicParsing="false" text="Upload" /> 
        </children> 
        <GridPane.margin> 
         <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> 
        </GridPane.margin> 
       </HBox> 
      </children> 
     </GridPane> 
    </center> 
</BorderPane> 

控制器类

package managefilesclientnetbeans; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.ListView; 
import javafx.scene.control.Tooltip; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.Pane; 
import managefilesclientnetbeans.sime.seven.ClientHandler; 


public class FXMLDocumentController implements Initializable { 

    ClientHandler clientHandler = new ClientHandler(); 
    ObservableList<String> content; 

    @FXML 
    public Label listLabel;  
    @FXML 
    public BorderPane root_borderPane; 
    @FXML 
    public Button refreshButton; 
    @FXML 
    public ListView<String> filesListView; 
    @FXML 
    public Button downloadButton; 
    @FXML 
    public Button uploadButton; 
    @FXML 
    public Pane previewPane; 
    @FXML 
    public Pane controlsPane; 


    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     assert root_borderPane != null : "fx:id=\"root_boarderPane\" was not " 
       + "injected check the FXML file"; 
     assert refreshButton != null : "refreshButton was not injected, check" 
       + " your FXML file"; 
     assert previewPane != null : "previewPane was not injected, check" 
       + " your FXML file"; 
     assert controlsPane != null : "controlsPane was not injected, check" 
       + " your FXML file"; 
     assert downloadButton != null : "downloadButton was not injected, check" 
       + " your FXML file"; 
     assert uploadButton != null : "uploadButton was not injected, check" 
       + " your FXML file"; 
     assert listLabel != null : "listLabel was not initialized, check " 
       + "your FXML file"; 

     content = FXCollections 
       .observableArrayList(clientHandler.getListOfFiles()); 
     for(String str : content) 
      System.out.println(str); 
     filesListView.setItems(content); //this line here cases the error 

//当注释掉应用程序启动,但点击按钮会导致//错误然后 断言filesListView = NULL!“filesListView未初始化,请检查“ +”您的FXML文件“; }

@FXML 
    public void onRefreshButtonClicked(MouseEvent event){ 
     filesListView.setItems(content); 
    } 
} 

应用程序类

由于线路
package managefilesclientnetbeans; 

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


public class ManageFilesClientNetBeans extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

     stage.setTitle("Remote Files Client"); 
     stage.getIcons().add(new Image(ManageFilesClientNetBeans.class.getResourceAsStream("images/synced.png"))); 
     stage.setScene(scene); 
     stage.show(); 
    } 

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

} 

堆栈跟踪

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:483) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303) 
    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:483) 
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$48/815033865.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: javafx.fxml.LoadException: 
file:/D:/vamk/TutorialWS/RESTAPIsWS/manageFilesClientNetBeans/dist/run1793002454/manageFilesClientNetBeans.jar!/managefilesclientnetbeans/FXMLDocument.fxml 

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091) 
    at managefilesclientnetbeans.ManageFilesClientNetBeans.start(ManageFilesClientNetBeans.java:23) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821) 
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/53700426.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$45/584634336.run(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/1879407574.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291) 
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/501263526.run(Unknown Source) 
    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$141(WinApplication.java:102) 
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source) 
    ... 1 more 
Caused by: java.lang.NullPointerException 

,这里是从按钮的堆栈跟踪点击

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762) 
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645) 
    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:54) 
    at javafx.event.Event.fireEvent(Event.java:198) 
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3437) 
    at javafx.scene.Scene$ClickGenerator.access$7900(Scene.java:3365) 
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3733) 
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452) 
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382) 
    at com.sun.glass.ui.View.handleMouseEvent(View.java:553) 
    at com.sun.glass.ui.View.notifyMouse(View.java:925) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102) 
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source) 
    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:483) 
    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:483) 
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757) 
    ... 30 more 
Caused by: java.lang.NullPointerException 
    at managefilesclientnetbeans.FXMLDocumentController.onRefreshButtonClicked(FXMLDocumentController.java:80) 
    ... 40 more 

回答

3

你做没有牛逼定义在一个FXML为fx:idfilesListView

+0

谢谢这就是问题所在,并分配ID后,它的工作原理。 – Sime