2016-04-02 30 views
-1

今私は、画面エラーのJava FXML

の間でパラメータを渡すしようとすると、私はいくつかの問題を持っているこれは私のログインコントローラです:

public class CustomerDetails { 
String name; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public int getAttendance() { 
    return attendance; 
} 

public void setAttendance(int attendance) { 
    this.attendance = attendance; 
} 

public double getTotalBill() { 
    return totalBill; 
} 

public void setTotalBill(double totalBill) { 
    this.totalBill = totalBill; 
} 
int attendance; 
double totalBill; 
:これは私のクラスのCustomerDetailsある

@FXML 
public void handleButtonAction(ActionEvent event) throws IOException{ 
    String text = textFieldLogIn.getText(); 
    Stage stage = new Stage(); 

    if(text.length() != 0){ 

     FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Menu.fxml")); 
     MenuController controller = fxmlLoader.<MenuController>getController(); 
     Parent root = (Parent)fxmlLoader.load(); 

     stage = (Stage) btnLogIn.getScene().getWindow(); 

     detail.setName("Tu"); 
      controller.setDetail(detail); 

     Scene scene = new Scene(root); 
     stage.setScene(scene); 


     stage.show(); 
    } 
    else{ 
     notiLogIn.setText("Please enter your name"); 
     notiLogIn.setTextFill(Color.RED); 
    } 

これは私のMenuControllerです。

public class MenuController implements Initializable { 
    CustomerDetails detail = new CustomerDetails(); 

public CustomerDetails getDetail() { 
    return detail; 
} 

public void setDetail(CustomerDetails detail) { 
    this.detail = detail; 
} 
@FXML 
private AnchorPane menuPane; 
@FXML 
private SplitPane menuSplitPane; 
@FXML 
private AnchorPane menuPane1; 
@FXML 
private VBox menuVBox; 
@FXML 
private Button btnFood; 
@FXML 
private Button btnDrink; 
@FXML 
private Button btnOther; 
@FXML 
private Button btnBill; 
@FXML 
private Label menuWelcome; 
@FXML 
private AnchorPane displayMenu; 

これで、getController()をcontroller.setDetail(detail)とともに使用しているときに、java.lang.NullPointerExceptionに関するエラーが発生し、その値をScreen 2:Menuに渡すことができないという問題が発生しました。だから私はこの問題を解決すべきですか?

答えて

0

質問はパラメータ渡しではありません。 controllerがnullであるためNullPointerExceptionが得られました。detailsがnullではないからです。

loadに電話する前に、getControllerでコントローラを取得します。

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Menu.fxml")); 
MenuController controller = fxmlLoader.<MenuController>getController(); 
Parent root = (Parent)fxmlLoader.load(); 

load方法はFXML文書からオブジェクト階層をロードし、また、この方法は、getController()によって取得することができるあなたのためのコントローラオブジェクトを作成します。

ソリューション:あなたはFXMLファイルロードした後、コントローラを得る:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Menu.fxml")); 
Parent root = (Parent)fxmlLoader.load(); 
MenuController controller = fxmlLoader.<MenuController>getController(); 
関連する問題