Init null array problem:
Hello All,
I just ran into something I hadn't seen before, I have a String[] that is:
String[] someArr = getSomeValuesMethod();
If the getSomeValuesMethod() returns null, then how do I populate the array with a pre-determined value, like a message?
The only thing I could get to work was to create an if block condition that if the array was null, then create a new String[] and initialize it with a message in the first field.
Any other ideas?
Thanks,
James
> Hello All,
> I just ran into something I hadn't seen before, I
> have a String[] that is:
> String[] someArr = getSomeValuesMethod();
> If the getSomeValuesMethod() returns null, then how
> do I populate the array with a pre-determined value,
> like a message?
>
> The only thing I could get to work was to create an
> if block condition that if the array was null, then
> create a new String[] and initialize it with a
> message in the first field.
>
> Any other ideas?
>
> Thanks,
> James
That's right, do something like this:
String[] someArr = getSomeValuesMethod();
if (someArr == null) {
someArr = <whatever your default value is>;
}
> This was a simple one, I got it:
> String[] someArr = getSomeValuesMethod();
> someArr = new String[] {msg};
>
> I just didn't see it the first few times through.
>
> Thx.
What's the point of assigning someArr on the first line if you're just going to reassign it to something else on the second?
> Sometimes the array will be null and other times it
> will not. It has to be discovered dynamically using
> the if block.
Riiight, but in reply 2 you gave code that always overwrites the value returned from getSomeValuesMethod(), not just when it's null. What's your question?