无法在javafx中设置静态TextField的值

时间:2017-11-20 23:32:27

标签: java exception javafx static textfield

我正在开发一个用于保险目的的程序,我有一个 textfield ,名为参考密钥每次我查看面板时都会随机生成此参考密钥其他场景和类需要这个参考键,所以我已经将它设置为静态,以便所有类都可以直接到达此实例!

我的问题是,当我为文本字段设置文本时,我甚至从包含文本字段的实例的同一个类中获得异常,但是震惊来了当我删除 TextField 实例的静态关键字时,它可以正常工作,并且文本也会改变!!

我的代码看起来像这样

@FXML
public static TextField ReferenceKey;
.
.
.
@Override
    public void initialize(URL url, ResourceBundle rb) {
ReferenceKey.setText("ABCD");
}

我得到的例外

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at database.project.SceneManager.LoadPanel(SceneManager.java:55)
    at database.project.HomePageController.lambda$initialize$2(HomePageController.java:83)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at database.project.CustmorInfoController.initialize(CustmorInfoController.java:100)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 37 more

我不知道为什么从底部的第4行开始说

  

引起:java.lang.NullPointerException

我确定我只导入了javafx的文本字段

是的,请有人帮助我 并提前感谢。

1 个答案:

答案 0 :(得分:1)

JavaFX不会将组件注入静态字段。如果可能,您应该传递此类的实例而不是使用静态字段。这可以通过允许使用此类的方法和类将此类作为参数来实现。您还可以使用PhD thesis获取控制器。

但是,如果需要,您可以拥有非静态字段并在initialize上设置静态字段:

@FXML
private TextField ReferenceKey;

public static TextField referenceKeyPublic;

...

@Override
public void initialize(URL url, ResourceBundle rb) {
    ClassName.referenceKeyPublic = ReferenceKey;
}