2017-02-21 153 views
0

我希望我的inventory_layout加載,當我點擊第一個按鈕,但我得到加載異常,而相同的代碼與其他fxmls工作正常。fxml中的加載異常

控制器的路徑是正確的。 fxml是在正確的地方。

這裏是我的代碼 - 庫存

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.scene.layout.AnchorPane; 
import javafx.scene.layout.Border; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.Pane; 
import javafx.stage.Modality; 
import javafx.stage.Stage; 
import pharmbooks.Utils.DbSingleton; 
import pharmbooks.Utils.UIController; 

import java.io.IOException; 

public class MainController { 


    @FXML 
    private BorderPane borderPane; 


    @FXML 
    public void initInventory(ActionEvent actionEvent) throws IOException{ 
     UIController<InventoryController> uiController=new UIController<InventoryController>(); 
     uiController.setUp(borderPane,"inventory_layout.fxml"); 

    } 


    public void initPurchase(ActionEvent actionEvent) throws IOException{ 
     UIController<PurchaseController> uiController=new UIController<>(); 
     uiController.setUp(borderPane,"purchase_layout.fxml"); 
    } 

} 

UI控制

public class UIController<T> { 

    private T controller; 
    private Pane pane; 
    public T getController() { 
     return controller; 
    } 


    public void setUp(Pane mainPane,String fxmlLocation){ 
     try { 
      FXMLLoader loader = new FXMLLoader(); 
      loader.setLocation(getClass().getClassLoader().getResource(fxmlLocation)); 
      pane = loader.load(); 

      controller = loader.getController(); 

      mainPane.getChildren().clear(); 
      mainPane.getChildren().add(pane); 

     }catch (Exception e){ 
      System.out.println(UIController.class.getSimpleName()+" "+e.toString()); 
     } 
    } } 

此代碼在Ubuntu效果很好,但:/

這裏是錯誤:

UIController的JavaFX .fxml.LoadException: /Users/manyamadan/Desktop/Everyth ING/pharmbooks-V2 /目標/類/ inventory_layout.fxml

庫存控制器:

公共類InventoryController實現Initializable {

public TableColumn batchId; 
public TableColumn company; 
public TableColumn name; 
public TableColumn packing; 
public TableColumn quantity; 
public TableColumn expiry; 
public TableColumn mrp; 

@FXML 
private TableView<Inventory> personTable; 

public void InventoryController() 
{ 

} 

public void initialize(URL location, ResourceBundle resources) { 




    batchId.setCellValueFactory(new PropertyValueFactory<Inventory, String>("BATCH_ID")); 
    company.setCellValueFactory(new PropertyValueFactory<Inventory, String>("COMPANY")); 
    name.setCellValueFactory(new PropertyValueFactory<Inventory, String>("PRODUCT_NAME")); 
    packing.setCellValueFactory(new PropertyValueFactory<Inventory, String>("PACKING")); 
    quantity.setCellValueFactory(new PropertyValueFactory<Inventory, Integer>("QUANTITY")); 
    expiry.setCellValueFactory(new PropertyValueFactory<Inventory, String>("EXPIRY_DATE")); 
    mrp.setCellValueFactory(new PropertyValueFactory<Inventory, String>("MRP")); 




    personTable.setEditable(true); 


    personTable.setItems(InventoryData.getInstance().getList()); 
    // name.setOnEditCommit(TableColumn.CellEditEvent(Inventory,String)event); 
    Inventory inventory_data = personTable.getSelectionModel().getSelectedItem(); 
    System.out.println(inventory_data.getPRODUCT_NAME()+ "" + inventory_data.getBATCH_ID()); 

    personTable.setOnKeyPressed(event -> { 
     TablePosition<Inventory, ?> pos = personTable.getFocusModel().getFocusedCell() ; 
     if (pos != null) { 
      AnchorPane anchorPane= null; 
      try { 
       anchorPane = FXMLLoader.load(getClass().getClassLoader().getResource("edit_inventory_data.fxml")); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      Stage stage=new Stage(); 
      stage.setTitle("Apna Dialog"); 
      stage.initModality(Modality.WINDOW_MODAL); 
      stage.initOwner(StageManager.getInstance().getPrimaryStage()); 

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

      stage.showAndWait(); 

     } 
    }); 



    name.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Inventory, String>>() { 
          @Override 
          public void handle(TableColumn.CellEditEvent<Inventory, String> t) { 
           System.out.println("ON edit commit" + t); 
           ((Inventory) t.getTableView().getItems().get(
             t.getTablePosition().getRow())).setPRODUCT_NAME(t.getNewValue()); 
          } 
         } 
    ); 
} 


@FXML 
public void addData(ActionEvent actionEvent) throws IOException { 
    AnchorPane anchorPane= FXMLLoader.load(getClass().getClassLoader().getResource("add_data_inventory.fxml")); 

    Stage stage=new Stage(); 
    stage.setTitle("Apna Dialog"); 
    stage.initModality(Modality.WINDOW_MODAL); 
    stage.initOwner(StageManager.getInstance().getPrimaryStage()); 

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

    stage.showAndWait(); 

    personTable.setItems(InventoryData.getInstance().getList()); 

} 

}

inventory_layout.fxml

<?import javafx.geometry.Insets?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.BorderPane?> 
<?import javafx.scene.layout.HBox?> 

<BorderPane maxHeight="600.0" maxWidth="800.0" prefHeight="600.0" prefWidth="800.0" stylesheets="@beautify_inventory.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pharmbooks.controllers.InventoryController"> 
    <center> 
     <TableView fx:id="personTable" prefWidth="10000.0" BorderPane.alignment="CENTER"> 
      <columns> 
       <TableColumn fx:id="batchId" prefWidth="75.0" text="BatchId" /> 
       <TableColumn fx:id="packing" prefWidth="75.0" text="packing" /> 
       <TableColumn fx:id="quantity" prefWidth="75.0" text="quantity" /> 
       <TableColumn fx:id="expiry" prefWidth="75.0" text="expiry" /> 
       <TableColumn fx:id="mrp" prefWidth="75.0" text="mrp" /> 
      </columns> 
     </TableView> 
    </center> 
    <top> 
     <HBox prefHeight="67.0" prefWidth="800.0" BorderPane.alignment="CENTER"> 
      <children> 
       <TextField prefHeight="47.0" prefWidth="163.00009155273438"> 
        <HBox.margin> 
         <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" /> 
        </HBox.margin> 
       </TextField> 
       <Button mnemonicParsing="false" onAction="#addData" text="Button"> 
        <HBox.margin> 
         <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" /> 
        </HBox.margin> 
       </Button> 
       <Button mnemonicParsing="false" prefHeight="39.0" prefWidth="121.0" text="Button"> 
        <HBox.margin> 
         <Insets bottom="20.0" right="20.0" top="20.0" /> 
        </HBox.margin> 
       </Button> 
       <Button mnemonicParsing="false" prefHeight="39.0" prefWidth="129.0" text="Button"> 
        <HBox.margin> 
         <Insets bottom="20.0" top="20.0" /> 
        </HBox.margin> 
       </Button> 
      </children> 
     </HBox> 
    </top> 
</BorderPane> 
+3

顯示異常堆棧跟蹤和'inventory_layout.fxml'代碼。 – MBec

+0

'InventoryController'中可能還有問題。 –

+0

嗨,請參閱編輯! @MBec – ALice

回答

0

inventory_layout.fxml中,您設置了fx:id屬性所有表列:

<TableColumn fx:id="batchId" prefWidth="75.0" text="BatchId" /> 
<TableColumn fx:id="packing" prefWidth="75.0" text="packing" /> 
<TableColumn fx:id="quantity" prefWidth="75.0" text="quantity" /> 
<TableColumn fx:id="expiry" prefWidth="75.0" text="expiry" /> 
<TableColumn fx:id="mrp" prefWidth="75.0" text="mrp" /> 

InventoryController你不@FXML註釋此組件。不知道這個註釋FXMLLoader將無法​​將正確的對象注入控制器。正確的代碼:

@FXML 
public TableColumn batchId; 
@FXML 
public TableColumn company; 
@FXML 
public TableColumn name; 
@FXML 
public TableColumn packing; 
@FXML 
public TableColumn quantity; 
@FXML 
public TableColumn expiry; 
@FXML 
public TableColumn mrp; 

我猜你NullPointerExceptioninitalize()方法,當您嘗試setCellValueFactory()表列。

我假定你正在訪問fxml資源文件。