0

I'm working with Java FX and I'm making and array of Text objects to display a text in different points on my GUI. I declare the array as follows:

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

At the very beginning of my start() method I try to make all of the Texts be blank like so:

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

but when I run this I get the following error:

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

The for loop above where I reference the Text array is on line 93. I think the compiler thinks the array is empty (Hence the nullpointer exception). Is there an easy way around this?

Thanks in advance!

Royginald
  • 125
  • 1
  • 10
  • Please show your code for initializing each element of that Text array. – MNEMO Dec 21 '21 at 02:58
  • @MNEMO I didn't initialize each element, I just tried setting the text of each one. Another person suggested doing something like this: ```for (Text text : texts) { text = new Text(); text.setText(""); }``` But that didn't work either – Royginald Dec 21 '21 at 03:42
  • 2
    I advise you take some time for additional study of foundational Java programming courses before writing JavaFX applications. Ensure that you use high quality training resources rather than relying on another person or stack overflow questions. – jewelsea Dec 21 '21 at 04:04

1 Answers1

2

It's because your array contains only null values. You need to populate the array with Text objects.

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

Below will cause the array to be initialized before used.

    public Text[] texts = { new Text(""), new Text(""),
                            new Text(""), new Text(""), 
                            new Text(""), new Text(""), 
                            new Text(""), new Text(""), 
                            new Text(""), new Text(""), };
Cheng Thao
  • 1,467
  • 1
  • 3
  • 9
  • I tried this but I got the same error message, but not where the Text objects are initialized, its later in my code when I try to set the massage to something else: ```texts[0].setText("Enter Message:");``` – Royginald Dec 21 '21 at 03:40
  • I updated the code. – Cheng Thao Dec 21 '21 at 03:46
  • For example, texts[0] and texts[1] are different object. If you initialized texts[0] but doesn't texts[1], texts[0].setText() call will be successful but texts[1].setText() won't. Check your code carefully and make sure your variables and constants are correctly written. – MNEMO Dec 21 '21 at 03:59
  • Yeah the updated code you sent were each individual Text object is initialized works perfectly, thank you! – Royginald Dec 21 '21 at 13:16