Java FX Text Array not Initalized

Hello Everyone, I’m working with Java FX and I’m making an array of Text objects to display text at 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 program on online compiler and 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!

Hi ankitdixit,

Please display the entire source file.

JerryC

Hi,
Java is a very much organized, object-situated language, which can be viewed as simple for fledglings. You can dominate it quickly, as there are many cycles that run consequently. You don’t need to dig into “how the things work in there” excessively profound. Java is a cross-stage language. It permits a software engineer to make an application, which can be conveyed on any gadget. It’s the language of decision for the Internet of Things and the right instrument for building endeavor level applications.

Hey, my name is Ellie, I recently completed my java training from SynergisticIT. It’s may be because of 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(""), };
1 Like

@ellie7090 is correct. Java initializes arrays with the default value for the type. For objects, the default value is null so you initialized an array of 10 null Text objects.

Basically you have to iterate and manually initialize all of the values using an appropriate constructor or factory method. For example, you could use an IntStream:

public Text[] texts = new Text[10];
IntStream.range(0, texts.length)
    .forEach(i -> texts[i] = new Text());

If you mostly work with arrays of primitives, you may not have come across this before since their default values are not null (eg. 0 for numbers and false for booleans).