I’m sort of new to programming so I’m sure this is an easy answer.
Basically, in my main method I want to do this:
…
wordtime = new LabelField("" + DataStored.words[0]);
…
Now in the DataStored class I have:
…
public static String[] words;
{
words = new String[2];
words[0] = "Oil sucks!";
words[1] = "Do you know that I can talk?";
}
…
So everytime I try to compile the program, it is getting stopped at the main class by saying that DataStored.words[0] is null. How can I get it to populate the fields from the other class? Thank you in advance!
You’re initializing
wordsin a non-static initialization block inDataStored. This means thatwordswon’t be initialized until you actually construct an instance ofDataStored, which is probably not what you want.You need to make the initialization block static, like this:
This will cause the block to be run when the
DataStoredclass is loaded.