2015-02-07 190 views
0

我一直在想如何處理另一個FXML文件中的FXML文件。但我有一個例外。 :( 這是我的內FXML(NewInside.fxml鑑於封裝):java.lang.NullPointerException在使用FXML處理FXML中的JavaFx中

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

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


<AnchorPane prefHeight="305.0" prefWidth="360.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.NewInsideController"> 
    <children> 
     <Button fx:id="newBtn" layoutX="30.0" layoutY="202.0" mnemonicParsing="false" onAction="#btnClick" text="Button" /> 
     <TextField fx:id="newTxt" layoutX="30.0" layoutY="134.0" prefHeight="25.0" prefWidth="280.0" /> 
     <Label fx:id="newLbl" layoutX="30.0" layoutY="62.0" prefHeight="17.0" prefWidth="280.0" /> 
    </children> 
</AnchorPane> 

這是它的控制器(NewInsideController.java在控制器封裝):

package controller; 

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

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.TextField; 

public class NewInsideController implements Initializable{ 

    @FXML public static Label newLbl; 
    @FXML public static TextField newTxt; 
    @FXML public static Button newBtn; 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 

    } 

    @FXML 
    private void btnClick(ActionEvent e){ 
     System.out.println("Button is clicked!"); 
     newLbl.setText(newTxt.getText()); 
    } 

} 

這是外FXML(鑑於包New.fxml):

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

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


<AnchorPane prefHeight="380.0" prefWidth="529.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.NewController"> 
    <children> 
     <fx:include source="NewInside.fxml" /> 
    </children> 
</AnchorPane> 

這是它的控制器(NewController.java在控制器封裝):

package controller; 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.fxml.Initializable; 

public class NewController implements Initializable { 

    /*@FXML private Label newLbl; 
    @FXML private Button newBtn; 
    @FXML private TextField newTxt; 
    */ 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 

    } 

    /*@FXML 
    public void btnClick(ActionEvent e){ 
     newLbl.setText(newTxt.getText()); 
    }*/ 

} 

最後,這是Main.java在應用程序包:

package application; 

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


public class Main extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     try { 
      Parent root = FXMLLoader.load(getClass().getResource("/view/New.fxml")); 
      Scene scene = new Scene(root); 
      scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

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

當我點擊按鈕,它給了我很長的例外: 「在線程異常‘的JavaFX應用程序線程的’java。 lang.RuntimeException:java.lang.reflect.InvocationTargetException .... ..... 由...引起:java.lang.NullPointerException at controller.NewInsideController.btnClick(NewInsideController.java:27) ... 57 more 「

請幫幫我! :(

+2

我只是個初學者,但只是一種猜測:從中刪除靜態字FXML變量? – Inge 2015-02-07 09:08:31

回答

1

由於@Inge表明,使用靜態字段不是做正確的方式。另外,代替public,你應該使用private。如果您需要公開您的控件,使用它們的屬性。

你可以閱讀爲什麼在這裏:

就在您的NewInsideController改變:如果你想有機會獲得從其他類的控件

@FXML private Label newLbl; 
@FXML private TextField newTxt; 
@FXML private Button newBtn; 

編輯

,而不是添加getter/setter方法暴露直接的控制,這是更好只是暴露他們的屬性。

舉例來說,有機會獲得這些控件的文本,你可以添加:

public StringProperty newLblText() { 
    return newLbl.textProperty(); 
} 
public StringProperty newTxtText() { 
    return newTxt.textProperty(); 
} 
public StringProperty newBtnText() { 
    return newBtn.textProperty(); 
} 

這將允許你從結合(或收聽的變化),獲取/設置文本屬性從它們中的任何控制器的外部。

要從NewController獲得NewInsideController的有效實例,請按照此link。首先修改New.fxml

<fx:include fx:id="newInside" source="NewInside.fxml" /> 

,現在你可以得到一個實例,並添加一些聽衆的性質:

public class NewController implements Initializable { 

    @FXML 
    private Parent newInside; 
    @FXML 
    private NewInsideController newInsideController; 

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

     newInsideController.newBtnText().addListener(
      (obs,s,s1)->System.out.println("nweBtn Text;" +s1)); 
     if(newInsideController.newLblText().get().isEmpty()){ 
      newInsideController.newLblText().set("Text for the textfield"); 
     } 
     newInsideController.newTxtText().addListener(
      (obs,s,s1)->System.out.println("s1 "+s1)); 
    }  
} 
+0

你是什麼意思「如果你需要暴露你的控件,使用他們的屬性」?你能解釋給我嗎? – Yin 2015-02-08 02:27:31

+0

我剛剛編輯了我的答案,添加了一個解釋。 – 2015-02-08 11:57:10

+0

我應該在哪裏放置最後一段代碼?它在控制器類中嗎? – Yin 2015-02-10 02:50:34