为什么我不能通过从另一个 Class 调用方法来使用 JavaFX 文本字段生成地图?

Why can't I generate a map using JavaFX textfields by calling the method from another Class?

*我是 java/programming 新手。

我编写了以下代码示例来尝试找出我的问题,然后给出我的问题的清晰示例。我想根据它们在应用程序中的功能将我的程序分成单独的 classes: 主要的 MainController - 驱动 JavaFX GUI。 初始化程序 - 输入数据的语义验证。 模型 - 对数据执行计算并将结果发送回 MainController。

我被卡住的地方是初始化程序,特别是 linkedhashmap 和第一次验证检查,抛出一个 nullpointerexception。如果所有内容都在一个 class 中,则不会发生这种情况(我从一个 class 中的所有内容开始,现在正在学习如何在单独的 class 中构建结构,等等)。如果我不使用任何 JavaFX 功能,它也不会发生。

例如,这个有效:

package helloWorld;
public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello world!");

        HashmapExample hashy = new HashmapExample();

        System.out.println(hashy.initialiser());
    }
}
package helloWorld;

import java.util.LinkedHashMap;
import java.util.Map;

public class HashmapExample {

    private Map<String,String> testMap = new LinkedHashMap<>();

    public void mapper() {

    testMap.put("one","a");
    testMap.put("two","b");
    testMap.put("three","c");

    }

    public String initialiser() {

        mapper();

        String errorField = testMap.entrySet().stream().filter(entry -> entry.getKey().isBlank()).map(Map.Entry::getValue).findFirst().orElse(null);

        String message;

        if (errorField != null) {
            return message = errorField + " blank!";
        } else {
            return message = "OK!";
        }

    }

}

但这不是:

package helloWorldFX;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            VBox root = (VBox)FXMLLoader.load(getClass().getResource("MainGUI.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
package helloWorldFX;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;

public class MainController {

    @FXML private TextArea outputMessage;

    public void run(ActionEvent event) {

        Initialiser initialiser = new Initialiser();

        outputMessage.setText(initialiser.checker());

    }

}
package helloWorldFX;

import java.util.LinkedHashMap;
import java.util.Map;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;

public class Initialiser {

    @FXML   private TextField one;
    @FXML   private TextField two;
    @FXML   private TextField three;

private Map<TextField,String> testMap = new LinkedHashMap<>();

    public void mapper() {

    testMap.put(one,"a");
    testMap.put(two,"b");
    testMap.put(three,"c");

    }

    public String checker() {

        mapper();

        String errorField = testMap.entrySet().stream().filter(entry -> entry.getKey().getText().isBlank()).map(Map.Entry::getValue).findFirst().orElse(null);

        String message;

        if (errorField != null) {
            return message = errorField + " blank!";
        } else {
            return message = "OK!";
        }

    }

}

当我在 eclipse 中覆盖 运行 时,我可以看出它可能是由行引起的 String errorField = testMap.entrySet().stream().filter(entry -> entry.getKey().isBlank()).map(Map.Entry::getValue).findFirst().orElse(null); 但我不是很确定。

我刚刚在场景生成器中注意到字段 'one'、'two' 和 'three' 没有显示为文本字段的有效 ID,因为它们不是在 MainController class 中。这一定与问题有关,但我不知道如何解决它。

FXML文件,按要求:请记住,它是一组示例代码,并非真正的程序,因此布局毫无意义。

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.VBox?>

<VBox prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="helloWorldFX.MainController">
   <children>
      <TextField />
      <TextField />
      <TextField />
      <Button mnemonicParsing="false" onAction="#run" text="Button" />
      <TextArea fx:id="outputMessage" prefHeight="200.0" prefWidth="200.0" />
   </children>
</VBox>

错误信息:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:203)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent(GlassViewEventHandler.java:447)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:411)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
    ... 46 more
Caused by: java.lang.NullPointerException
    at helloWorldFX.Initialiser.lambda[=17=](Initialiser.java:29)
    at java.base/java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:176)
    at java.base/java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1812)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:543)
    at helloWorldFX.Initialiser.checker(Initialiser.java:29)
    at helloWorldFX.MainController.run(MainController.java:15)
    ... 58 more

解决方法:

因此,根据评论和对可能原因的更多思考,我想出了这个解决方法。

我在 MainController 中实例化并填充 Map,然后实例化 Initialiser class,它现在通过构造函数将 Map 作为参数。然后我从 Initialiser 对象调用检查器方法,它对实例化时发送到对象的数据运行语义检查。

据我所知,问题是一个 fxml 文件只能处理一个控制器 class,所以你不能从除主控制器之外的任何地方用 TextFields 填充地图控制器 class。如果这样做,getText 方法将抛出 nullpointerexception,因为它指向的 TextFields 在技术上不存在于 fxml 中。

查看修改后的代码:

package helloWorldFX;

import java.util.LinkedHashMap;
import java.util.Map;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.event.ActionEvent;

public class MainController {

    @FXML   private TextField one;
    @FXML   private TextField two;
    @FXML   private TextField three;

    @FXML   private TextArea outputMessage;

    public void run(ActionEvent event) {

        Map<TextField, String> testMap = new LinkedHashMap<>();

        testMap.put(one,"a");
        testMap.put(two,"b");
        testMap.put(three,"c");

        Initialiser initialiser = new Initialiser(testMap);

        outputMessage.setText(initialiser.checker());

    }

}
package helloWorldFX;

import java.util.Map;

import javafx.scene.control.TextField;

public class Initialiser {

    private Map<TextField, String> values;

    public Initialiser(Map<TextField, String> mappedValues) {
        this.values = mappedValues;
    }

    String message;

    public String checker() {

        String errorField = values.entrySet().stream().filter(entry -> entry.getKey().getText().isBlank()).map(Map.Entry::getValue).findFirst().orElse(null);

        if (errorField != null) {
            return message = errorField + " blank!";
        } else {
            return message = "OK!";
        }

    }

}

在这一点上,由于我缺乏经验,我不知道这是否真的是一种解决方法或真正正确的方法 - 也许有人可以帮助填写这部分内容。