2016-10-21 120 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