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 OverflowYou 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";
Use with AIString[] args = new String[]{"firstarg", "secondarg", "thirdarg"};
You could do something like this
String[] myStrings = { "One", "Two", "Three" };
or in expression
functionCall(new String[] { "One", "Two", "Three" });
or
String myStrings[];
myStrings = new String[] { "One", "Two", "Three" };
By using the array initializer list syntax, ie:
String myArray[] = { "one", "two", "three" };
You have forgotten to give the variable a name? Both of these work.
Use with AIString[][] array = {emptyArr, filledArr, moreFilledArr};
Use with AIString[][] array = new String[][] {emptyArr, filledArr, moreFilledArr};
I recommend you to go through the basic Java syntax specification and tutorials. Start with The Java Tutorials by Oracle Corp, free of cost.
these two are right too String[][] array = {emptyArr, filledArr, moreFilledArr};
Use with AIString[][] array = new String[][] {emptyArr, filledArr, moreFilledArr};
please do check these too
Docs for Array in Java Have a Look for more clarification
tutorial of array with java docs
,
I had to add this as a comment but I couldn't, I want to extend with more resources mentioned above , Thanks
Use this code:
String[] Year = new String[50]; //Initialize the year string
Now Java knows how long you want the array to be, so it can initialize it.
If you don't tell Java how long you want your array to be, it can't make the array because it doesn't know how long the array should be.
Also, you could use a final variable SIZE if you want to adjust it easily.
Furthermore, you don't need the string j since you only use it to store the result of something before adding it to the array.
This will not affect your code, but the correct Java naming convention is that variables should start with a lowercase letter, so change Year to years.
Therefore, your code could be improved like this:
int year = 1995; //the current year i'm working with
final int SIZE = 50; //how big
String[] years = new String[SIZE]; //Initialize the year string
for(int i=(year-SIZE); i < year; i++) //Note the use of the SIZE variable
{
years[i] = Integer.toString(i);
}
U didn;t Initilize Year Varible, also, there is not Items inside the Year Array
String[] Year = new String[50]; // Initlaize adn add Some content
Because you can not define dimension expressions when an array initializer is provided.
That means that you can either specify the dimension or the array initializer (i.e. the values). You can't specify both at the same time.
Your second initalization is the correct one.
The first syntax is not valid because you are declaring the size of the array twice:
- once explicitly
- another time because the array of strings between { and } has size 1
Doing the same thing twice can introduce potential errors so the java designers decided to permit only the second approach.
Because you can not define dimension expressions when an array initializer is provided.
That means that you can either specify the dimension or the array initializer (i.e. the values). You can't specify both at the same time.
Your second initalization is the correct one.
The first syntax is not valid because you are declaring the size of the array twice:
- once explicitly
- another time because the array of strings between { and } has size 1
Doing the same thing twice can introduce potential errors so the java designers decided to permit only the second approach.