试图迭代文本字段

时间:2017-10-25 22:38:20

标签: java javafx fxml

我试图用forloop获取每个TextField的文本。 我尝试的是: 创建包含其中每个文本字段的TextField数组。 然后在有人按下按钮后我将进入一个for循环,它应该得到每个文本字段的值,但这不起作用,它给了我一个错误(我在帖子的末尾发布)。 当我选择一个文本字段时,它确实有效。像inputName.getText();然后它确实返回TextField的值,但当我尝试使用像textFields [i] .getText();它给了我一个错误。 这是一个JavaFX FXML应用程序 这是FXMLDocumentController.java文件

package justTesting;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class FXMLDocumentController implements Initializable {

    @FXML
    private TextField 
            inputName, inputAddress, inputCity, inputPostalcode, inputCountry, inputNumber, inputMail,
            inputLabel, inputFlight, inputDestination, 
            inputType, inputBrand, inputColor, inputSpecial;

    @FXML
    private TextField[] textFields =  {
            inputName, inputAddress, inputCity, inputPostalcode, inputCountry, inputNumber, inputMail,
            inputLabel, inputFlight, inputDestination, 
            inputType, inputBrand, inputColor, inputSpecial};

    @FXML
    private String[] dataTypes = {"Name: ", "Address: ", "City: ", "Postalcode: ", "Country: ", "Phone Number: ", "E-Mail: ",
                                  "Label Tag: ", "Flight number: ", "Destination: ", 
                                  "Type: ", "Brand: ", "Color: ", "Special characteristics: "};

    @FXML
    private Label label;

    @FXML
private void getInputButton(ActionEvent event) {

    System.out.println("Dit is alleen ter verduidelijking voor de designers");
    for (int i = 0; i < textFields.length; i++) {
        System.out.println(textFields[i].getText());
//the following line causes the error ( textFields[i].getText() )
                System.out.printf("%-20s%10s\n", dataTypes[i], textFields[i].getText());
    }
}

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

有谁知道获取每个文本字段值的正确方法(使用forloop是什么?)

它给我的错误:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    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:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    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$MouseHandler.process(Scene.java:3757)
    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.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$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 52 more
Caused by: java.lang.NullPointerException
    at lostandfoundluggageform.FXMLDocumentController.getInputButton(FXMLDocumentController.java:47)
    ... 62 more

1 个答案:

答案 0 :(得分:1)

初始化initialize()方法中的文本字段数组。在初始化数组时(在类初始值设定项中),FXMLLoader不能注入文本字段,并且仍然为空。因此,您的数组只是充满了空引用。

假设您未发布的代码中没有错误,则以下内容应该有效:

public class FXMLDocumentController implements Initializable {

    @FXML
    private TextField 
            inputName, inputAddress, inputCity, inputPostalcode, inputCountry, inputNumber, inputMail,
            inputLabel, inputFlight, inputDestination, 
            inputType, inputBrand, inputColor, inputSpecial;

    private TextField[] textFields ;

    // ...

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        textFields = new TextField[] {
            inputName, inputAddress, inputCity, inputPostalcode, inputCountry, inputNumber, inputMail,
            inputLabel, inputFlight, inputDestination, 
            inputType, inputBrand, inputColor, inputSpecial} ;

    }

}