【问题标题】:Java FX Text array not Initalized [duplicate]Javafx文本数组未初始化[重复]
【发布时间】:2022-01-22 13:25:08
【问题描述】:

我正在使用 Java FX,并且正在制作 Text 对象数组以在我的 GUI 上的不同点显示文本。我声明数组如下:

public Text[] texts = new Text[10];

在我的 start() 方法的最开始,我尝试让所有的文本都是空白的,如下所示:

for (Text text : texts) 
    text.setText("");

但是当我运行它时,我收到以下错误:

Exception in Application start method
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:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
    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:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
    at sample.Main.start(Main.java:93)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
    ... 1 more
Exception running application sample.Main

上面我引用 Text 数组的 for 循环位于第 93 行。我认为编译器认为该数组为空(因此出现空指针异常)。有没有简单的方法解决这个问题?

提前致谢!

【问题讨论】:

  • 请展示您的代码以初始化该文本数组的每个元素。
  • @MNEMO 我没有初始化每个元素,我只是尝试设置每个元素的文本。另一个人建议做这样的事情:for (Text text : texts) { text = new Text(); text.setText(""); } 但这也不起作用
  • 我建议您在编写 JavaFX 应用程序之前花一些时间额外学习基础 Java 编程课程。确保您使用高质量的培训资源,而不是依赖其他人或堆栈溢出问题。

标签: java arrays javafx text


【解决方案1】:

这是因为你的数组只包含空值。您需要使用 Text 对象填充数组。

public Text[] texts = new Text[10];
for(int i=0; i<texts.length; i++){
  texts[i] = new Text();
  texts[i].setText("");
}

下面会导致数组在使用前被初始化。

    public Text[] texts = { new Text(""), new Text(""),
                            new Text(""), new Text(""), 
                            new Text(""), new Text(""), 
                            new Text(""), new Text(""), 
                            new Text(""), new Text(""), };

【讨论】:

  • 我试过这个,但我得到了相同的错误消息,但不是 Text 对象的初始化位置,当我尝试将按摩设置为其他内容时,它在我的代码中稍后:texts[0].setText("Enter Message:");
  • 我更新了代码。
  • 例如 texts[0] 和 texts[1] 是不同的对象。如果您初始化 texts[0] 但未初始化 texts[1],则 texts[0].setText() 调用将成功,但 texts[1].setText() 不会。仔细检查你的代码,确保你的变量和常量写得正确。
  • 是的,您发送的更新代码是每个单独的 Text 对象都已初始化完美,谢谢!
猜你喜欢
  • 2018-11-24
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2016-08-08
  • 2014-10-13
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多