You need to initialize errorSoon, as indicated by the error message, you have only declared it.

Use with AIString[] errorSoon;                   // <--declared statement
String[] errorSoon = new String[100]; // <--initialized statement

You need to initialize the array so it can allocate the correct memory storage for the String elements before you can start setting the index.

If you only declare the array (as you did) there is no memory allocated for the String elements, but only a reference handle to errorSoon, and will throw an error when you try to initialize a variable at any index.

As a side note, you could also initialize the String array inside braces, { } as so,

Use with AIString[] errorSoon = {"Hello", "World"};

which is equivalent to

Use with AIString[] errorSoon = new String[2];
errorSoon[0] = "Hello";
errorSoon[1] = "World";
Answer from Anthony Forloney on Stack Overflow
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-string-array
Java String Array: Declaration, Initialization & Examples | DigitalOcean
August 3, 2022 - Master Java String arrays with declaration, initialization, and manipulation examples. Learn iteration, sorting, searching, and conversion techniques.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ string-arrays-in-java
String Arrays in Java - GeeksforGeeks
October 2, 2025 - // Java program to demonstrate ... "good", "girl" }; // Create a StringBuilder to build the resulting string StringBuilder sb = new StringBuilder(); // Append the first element of the array to the StringBuilder sb.append(arr[0]); ...
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ initialize an array of string in java
Initialize an array of String in Java | Techie Delight
July 7, 2026 - 1. We can declare and initialize an array of string in Java by using a new operator with an array initializer.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ how-to-initialize-string-array-in-java
How to Initialize String Array in Java?
January 12, 2021 - To initialize String Array in Java, define a string array and assign a set of elements to the array, or define a string array with specific size and assign values to the array using index.
Find elsewhere
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ java how to initialize string[]?
Java how to initialize String[]? | Codemia
January 27, 2025 - There are multiple ways to initialize a String[] in Java: At declaration: This method involves directly specifying the elements of the array at the time of its declaration.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ java arrays โ€บ string array in java
String Array in Java
May 11, 2023 - Today we are going to look at string arrays in Java language, that is, arrays, each element of which is a string. We'll figure out how to declare Java String Array and how to work with it. You can declare and initialize a String array in Java in different ways.
๐ŸŒ
Software Testing Help
softwaretestinghelp.com โ€บ home โ€บ java โ€บ java string array- tutorial with code examples
Java String Array- Tutorial With Code Examples
April 1, 2025 - This tutorial on Java String Array explains how to declare, initialize & create String Arrays in Java and conversions that we can carry out on String Array.
๐ŸŒ
HappyCoders
happycoders.eu โ€บ home โ€บ java โ€บ how to declare and initialize arrays in java
How to Declare and Initialize Arrays in Java
September 24, 2026 - We do not yet specify the size of the array in the declaration. The array variables (in the example intArray and stringArray) do not contain the array itself but a reference to an array, which is ultimately an object on the Java heap. Only when we create the array object itself do we have to define its size.
๐ŸŒ
W3docs
w3docs.com โ€บ home โ€บ code snippets โ€บ java โ€บ java: how to initialize string[]?
Java: how to initialize String[]? | W3docs
Here are a few examples: // method 1: using an array literal String[] arr1 = {"apple", "banana", "cherry"}; // method 2: using the new operator String[] arr2 = new String[] {"apple", "banana", "cherry"}; // method 3: using the new operator and ...
Top answer
1 of 7
124

There are various options. Personally I like using Guava:

List<String> strings = Lists.newArrayList("s1", "s2", "s3");

(Guava's a library worth having anyway, of course :)

Using just the JDK, you could use:

List<String> strings = Arrays.asList("s1", "s2", "s3");

Note that this will return an ArrayList, but that's not the normal java.util.ArrayList - it's an internal one which is mutable but fixed-size.

Personally I prefer the Guava version as it makes it clear what's going on (the list implementation which will be returned). It's also still clear what's going on if you statically import the method:

// import static com.google.common.collect.Lists.newArrayList;
List<String> strings = newArrayList("s1", "s2", "s3");

... whereas if you statically import asList it looks a little odder.

Another Guava option, if you don't want a modifiable-in-any-way list:

ImmutableList<String> strings = ImmutableList.of("s1", "s2", "s3");

I typically want to either have a completely mutable list (in which case Lists.newArrayList is best) or a completely immutable list (in which case ImmutableList.of is best). It's rare that I really want a mutable-but-fixed-size list.

2 of 7
42

Java 9+

Java 9 introduces a convenience method List.of used as follows:

List<String> l = List.of("s1", "s2", "s3");

Java 8 and older

Here are a few alternatives:

// Short, but the resulting list is fixed size.
List<String> list1 = Arrays.asList("s1", "s2", "s3");

// Similar to above, but the resulting list can grow.
List<String> list2 = new ArrayList<>(Arrays.asList("s1", "s2", "s3"));

// Using initialization block. Useful if you need to "compute" the strings.
List<String> list3 = new ArrayList<String>() {{
    add("s1");
    add("s2");
    add("s3");
}};

When it comes to arrays, you could initialize it at the point of declaration like this:

String[] arr = { "s1", "s2", "s3" };

If you need to reinitialize it or create it without storing it in a variable, you do

new String[] { "s1", "s2", "s3" }

If the string constants are may though, it would look like

String[] arr = { "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10",
                 "s11", "s12", "s13" };

In these cases I usually prefer writing

String[] arr = "s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12,s13".split(",");
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-array โ€บ java-string-array
Java String Array - Examples
November 23, 2020 - Following is the syntax to declare an Array of Strings in Java. ... You can use any of these two notations. To initialize a string array, you can assign the array variable with new string array of specific size as shown below.
๐ŸŒ
Hero Vired
herovired.com โ€บ learning-hub โ€บ blogs โ€บ string-array-in-java
String Array in Java: How to Declare, Example, Initializing
October 7, 2024 - Java String Arrays are a powerful data type in Java that can store an array of strings. They can be initialized with literal values, dynamically created, or parsed from a string using the split() method.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ java-array-how-to-declare-and-initialize-an-array-in-java-example
Java Array โ€“ How to Declare and Initialize an Array in Java Example
February 4, 2022 - String[] names = {"John", "Jade", "Love", "Allen"}; ... We have initialized our arrays by passing in values with the same data type with each value separated by a comma. If we wanted to access the elements/values in our array, we would refer ...
๐ŸŒ
Blogger
self-learning-java-tutorial.blogspot.com โ€บ 2020 โ€บ 04 โ€บ how-to-initialize-string-array_27.html
Programming for beginners: How to initialize string array?
Approach 2: By defining the array and initialize elements using index notation. ... Approach 5: Using streams. String[] str5 = Arrays.asList("Hello", "How", "Are", "You").stream().toArray(String[]::new); Find the below working application. ... package com.sample.app; import java.util.Arrays; ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ java โ€บ how to initialize an array in one line in java
Java Initialize an Array in One Line with Examples | Sentry
September 17, 2026 - public class Main { public static void main(String[] args) { String[] words = new String[4]; words[0] = "java"; words[1] = "arrays"; words[2] = "are"; words[3] = "easy"; System.out.println(words[0]); System.out.println(words[1]); } } However, sometimes you have all the data you need when you initialize the array, and you want to initialize the array using less code.