2016-10-21 123 views
0
public class AccountOverviewController { 

//declarations ... 

@FXML 
private Button undoButton; 

@FXML 
private Button redoButton; 

@FXML 
private void initialize() { 
    transactionsTable.getSelectionModel().setSelectionMode(
      SelectionMode.MULTIPLE); 

    dateColumn.setCellValueFactory(dataValue -> dataValue.getValue() 
      .getDateProperty()); 
    payeeColumn.setCellValueFactory(dataValue -> dataValue.getValue() 
      .getPayeeProperty()); 
    categoryColumn.setCellValueFactory(dataValue -> dataValue.getValue() 
      .getCategoryProperty()); 
    inflowColumn.setCellValueFactory(dataValue -> dataValue.getValue() 
      .getInflowProperty()); 

    deleteButton.disableProperty().bind(Bindings.isEmpty(transactionsTable.getSelectionModel().getSelectedItems())); 

    editButton.disableProperty().bind(Bindings.size(transactionsTable.getSelectionModel().getSelectedItems()).isNotEqualTo(1)); 

    undoButton.disableProperty().bind(Bindings.isEmpty(commandRegistry.getCommandStack())); 
    redoButton.disableProperty().bind(Bindings.isEmpty(commandRegistry.getUndoCommandStack())); 
} 

//handlers&setters ... 
} 

這兩行在最後引起錯誤。我想在命令堆棧爲空時禁用按鈕。我不知道爲什麼。例如,對於刪除/編輯按鈕禁用相同的按鈕可以正常工作。沒有這兩個完整的應用程序完美的罰款。JavaFX按鈕綁定異常

異常鏈:

javafx.fxml.LoadException: 
/home/simon/eclipse/java-neon-workspace/to2/lab2/cw3/bin/pl/edu/agh/iisg/to/javafx/cw3/view/AccountOverviewPane.fxml 

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409) 
    at pl.edu.agh.iisg.to.javafx.cw3.presenter.AccountPresenter.initRootLayout(AccountPresenter.java:35) 
    at pl.edu.agh.iisg.to.javafx.cw3.Main.start(Main.java:20) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$106(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$119(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$117(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$118(PlatformImpl.java:294) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) 
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$450(GtkApplication.java:139) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) 
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566) 
    ... 13 more 
Caused by: java.lang.NullPointerException 
    at pl.edu.agh.iisg.to.javafx.cw3.view.AccountOverviewController.initialize(AccountOverviewController.java:97) 
    ... 23 more 

棧兩者都宣稱像這樣CommandRegistry

private ObservableList<Command> commandStack = FXCollections.observableArrayList(); 

和getter自然迴歸自己。這裏可能會出現什麼問題?

+0

能否請您添加對'AccountOverviewController'完整的代碼?你的蹤跡說'NullPointerException',你確定'commandRegistry'被初始化了嗎? – beatngu13

+0

那麼哪條線是97線? –

+0

在這個特殊的例子中,它是用'undoButoon.disablePrope ...'。但是他們每個人都會造成同樣的異常鏈。這裏是整個[項目](https://github.com/Sukiennik/JavaFX-Projects/tree/master/pl/edu/agh/iisg/to/javafx/cw3)和這裏[AccountOverviewController](http:// pastebin .com/cvGRY5z5) – Saris

回答

1

看你project,尤其是AccountOverviewControllerAccountPresenter後,我說你拿到NullPointerException因爲控制器嘗試類有CommandRegistry實例前內initialize()訪問commandRegistry

看一看線38 - 41 AccountPresenter

AccountOverviewController controller = loader.getController(); 
controller.setPresenter(this); 
controller.setData(DataGenerator.generateAccountData()); 
controller.setCommandRegistry(commandRegistry); 

你創建你的控制器,並設置commandRegistry之後。但在調用AccountOverviewController的構造函數後,直接調用initialize()(有關詳細信息,請查看this問題)。目前,commandRegistrynull。解決這個問題

一種方式是通過綁定移動到二傳手:

public void setCommandRegistry(CommandRegistry commandRegistry) { 
    this.commandRegistry = commandRegistry; 

    undoButton.disableProperty().bind(Bindings.isEmpty(commandRegistry.getCommandStack())); 
    redoButton.disableProperty().bind(Bindings.isEmpty(commandRegistry.getUndoCommandStack())); 

    commandLogView.setItems(commandRegistry.getCommandStack()); 
    commandLogView.setCellFactory(lv -> new ListCell<Command>() { 
     protected void updateItem(Command item, boolean empty) { 
      super.updateItem(item, empty); 
      setText((item != null && !empty) ? item.getName() : null); 
     }; 
    }); 
} 
+0

謝謝,我現在明白了。那麼綁定如何在'transactionsTable'上工作,如果它在調用控制器期間還沒有被初始化。例如'editButton'或'deleteButton'。 – Saris

+1

@Saris因爲所有的依賴已經存在。 'editButton'和'deleteButton'需要'transactionTable'。所有這些字段都用'@ FXML'註釋,這在之前被調用過。所有你必須記住的是這個執行順序:1.構造函數,2. @ @ FXML,3.'initialize()'。 – beatngu13

+0

感謝您的幫助和耐心。 – Saris