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

dlammy

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.

Infuzion

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;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JavaFX Set an Input filter by Pattern on a TextField

Value Change Listener for JavaFX's TextField

How to eliminate chars in textfield JavaFx directly so the user can't type in chars?

Set KeyPressed event for a TextField in JavaFX

set TextField prompt text on runtime JavaFx

Can't set a value on a cell

Flutter set value to a textfield

Set value of TextField from another window in javafx

How can I set an static outlined div similar to Material-UI's outlined textfield?

addEventListener to an element in a array doesn't set the expected value in the textfield - Javascript

How can I set the value of my MaterialUI TextField to uppercase?

can't get the dynamic textfield value using Jquery

use uidatepicker to set textfield value

How to limit max value in textfield with handler javafx

How to set TextField on the top using Javafx?

JavaFX can't set WebView in FXML file

Ubuntu can't set IP to static

Can't Set variable value in Static Field

JavaFX TextField Array max length of text value

JavaFX : TextField max and min value by listener

Can't set property value

Set value of textfield with javascript

set caretPosition to the right in a textField (javaFX)

Can't set a variable to the value of a textfield

JavaFX can't set the text of Label or Text

In JavaFX, Is it possible to enable a CheckBox when the TextField's value is not set to Zero

Can't backspace first character in JavaFX TextField after applying a Text formatter

useEffect to set value of TextField not working

react set value to a textfield