2017-05-18 16 views
0

Ich habe Scene Builder installiert und ein neues FXML-Projekt geöffnet, nur um es zu testen. Ich habe auf anderen Computern gesehen, dass beim Öffnen eines neuen FMXL-Projekts der Standardcode eine Schaltfläche generiert, die Text beim Klicken anzeigt. Wenn ich jedoch versuche, die Hauptmethode des Standard-FXML-Projekts auszuführen, erhalte ich einen Stapelfehler mit einer NullPointerException. Ich habe viele Fragethreads durchsucht, aber es gibt keine konkrete Antwort. Jede Hilfe wäre willkommen.Scene Builder Ausnahme in der Anwendung (NullPointerException)

## JavaFXApplication2.java ## 

package javafxapplication2; 

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

/** 
* 
* @author Hafiz 
*/ 
public class JavaFXApplication2 extends Application { 

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

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

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

## FXMLDocumentController.java ## 

} 
package javafxapplication2; 

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; 

/** 
* 
* @author Hafiz 
*/ 
public class FXMLDocumentController implements Initializable { 

    @FXML 
    private Label label; 

    @FXML 
    private void handleButtonAction(ActionEvent event) { 
     System.out.println("You clicked me!"); 
     label.setText("Hello World!"); 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
     // TODO 
    }  

} 

## FXMLDocument.fxml ## 

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

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

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication2.FXMLDocumentController"> 
    <children> 
    <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" /> 
    <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" /> 
</children> 

Antwort

0

Ich denke, dass Ihr Problem durch private Methode "handleButtonAction" verursacht wird. Mach es öffentlich und versuche es dann. Grüße

Verwandte Themen