Joey John John
Joey John John

Reputation: 243

Calling a string[] from another class is appearing null

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!

Upvotes: 2

Views: 609

Answers (5)

Avogadro
Avogadro

Reputation: 363

public static String[] words; { words = new String[2];

words[0] = "Oil sucks!";
words[1] = "Do you know that I can talk?"; }

If you want to use this as a method for initializing the words instead of declaring an array of String's then add parathesis after words in the method header and remove the semi-colon.

Upvotes: -1

Rocky
Rocky

Reputation: 46

If you define the static block initializing these two variable in Class DataStored. You can access it.

Class DataStored {

public static String[] words;

Static {

words[0] = "Oil sucks!";
words[1] = "Do you know that I can talk?";

}

}

Upvotes: 1

DNA
DNA

Reputation: 42597

If you want to initialize words properly you need to do it in a static block, which is run as soon as the class is loaded. An initializer block, which you have used, is only run when an instance of the class is created, just before the constructor is called. So when you call DataStored.words[0], the initializer hans't been run, and words is still null.

public static String[] words;

static
{
    words = new String[2];

    words[0] = "Oil sucks!";
    words[1] = "Do you know that I can talk?";
}

Or you can simply do:

 public static String[] words = new String[]{"Oil sucks!","Do you know that I can talk?"};

Or shorter still:

 public static String[] words = {"Oil sucks!","Do you know that I can talk?"};

Upvotes: 1

Mike Daniels
Mike Daniels

Reputation: 8642

You're initializing words in a non-static initialization block in DataStored. This means that words won't be initialized until you actually construct an instance of DataStored, which is probably not what you want.

You need to make the initialization block static, like this:

public static String[] words;
static { // <---------------------
    words = new String[2];

    words[0] = "Oil sucks!";
    words[1] = "Do you know that I can talk?";
}

This will cause the block to be run when the DataStored class is loaded.

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500395

You've written an initializer block there - it will be executed every time you create a new instance of DataStored, but not until that time. I suspect you want:

public static String[] words = { "Oil sucks!", "Do you know that I can talk?" };

It's also a bad idea to have public fields like this, at least for anything other than constants (which would include immutable types in my view).

(I doubt that this is failing at compile time though - it's an execution-time failure. It's worth being clear about the difference.)

Upvotes: 6

Related Questions