2013-07-30 39 views
0

我試圖從不同的包中訪問.fxml時遇到問題。可以說我的login.fxml在客戶包中。成功登錄後,它將轉到主頁。在主頁上,我收到了一個按鈕,並鏈接到購物車包中的myShoppingCart.fxml。空指針異常當切換窗口

public void goToCart(ActionEvent event) { 
    String userName = CustomerLoginController.userLoggedIn.getName(); 
    try { 
     FXMLLoader fxmlLoader = new FXMLLoader(); 
     Pane p = (Pane) fxmlLoader.load(getClass().getResource("cartCustHome.fxml").openStream()); 
     CartCustHomeUI fooController = (CartCustHomeUI) fxmlLoader.getController(); 
     fooController.retrieveUserName(userName); 
     Stage stage = new Stage(); 
     Scene scene = new Scene(p); 
     scene.getStylesheets().add(MainFrame.class.getResource("cart.css").toExternalForm()); 
     stage.setScene(scene); 
     stage.show(); 
     ((Node) (event.getSource())).getScene().getWindow().hide(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

這不是讓我cartCustHome.fxml eventhough我已經導入我的購物車包:不過,我當我做了這個空指針異常錯誤。我想知道爲什麼會這樣。提前致謝。

+0

myShoppingCart.fxml和cartCustHome.fxml之間的關係是什麼?異常發生在哪一行?搜索網絡爲「java getresource返回null」。 –

+0

他們在不同的包裝 –

+0

您的按鈕鏈接到myShoppingCart.fxml,但它的行動iin試圖加載cartCustHome.fxml?無論如何嘗試'getClass()。getResource(「../ customer/cartCustHome.fxml」)。openStream()'。 –

回答

1

嘗試

getClass().getResource("../customer/cartCustHome.fxml").openStream() 

欲瞭解更多信息,請搜索網站的 「java的getResource返回null」。關於這個話題有很多討論,並提供了全面的解釋。

+0

但我認爲它與getClass()。getResource(「/ customer/cartCustHome.fxml」)。openStream();非常感謝 –