Can't set the value of a static TextField in javafx [duplicate]

喜你入骨 提交于 2020-01-11 14:10:16

问题


I'm working on a program for Insurance purposes and there I've a textfield called Reference Key this reference key is generated randomly every time I View the panel the other Scenes and classes needs this Reference Key , so I've made it static so that all the classes can reach this Instance directly !

My problem is that when I set the text for the textfield I get an Exception even from the same class that contains the instance of the textfield,but the shocks comes when I remove the static Keyword for the TextField instance it works perfectly and the text for it changes !!

My code looks like this

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

the exception that I got

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

I don't know why at the 4th line from the bottom it says

Caused by: java.lang.NullPointerException

I'm sure that I've imported only the Textfields for the javafx

Could someone help me please and thanks in Advance.


回答1:


JavaFX does not inject components into static fields. If possible, you should pass an instance of this class around instead of using static fields. This can be accomplished by allowing methods and class using this class to take this class as a parameter. You are also able to get the controller using FXMLLoader#getController.

However, if necessary, you can have a non-static field and set a static field on initialize:

@FXML
private TextField ReferenceKey;

public static TextField referenceKeyPublic;

...

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


来源:https://stackoverflow.com/questions/47402865/cant-set-the-value-of-a-static-textfield-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!