I have a strange problem with Strings and I cannot understand why. I suspect variable scope but since I am not sure, I am posting here for help.
I have a class method that must return a double dimensional String array. The variable I want to return is named "dataToReturn".
The method works fine according to its task. I have put various "println" during method execution and I am positive that the "dataToReturn" is populated correctly.
But at the end of the method, when I am returning the array, all array elements are null, so my program crashes with NullExceptionPointer because I need to work on the returned array.
Can you please help me?
Is it something with scoping issues?
I am reading some XML values in the first big for loop and I am assingning values to the array elements (I am checking that they are correctly assigned with "println" statements). Maybe when I come out of the for loop, these array elements which are String objects, are not present any more??
Line 30 is overwriting the array at location s for each single element. As a result, the array with the values you already set will be tossed away, and an empty one (with only null values) will be put in its place. At most one value will then be set. You should move its declaration and initialization forward:
That still allows the possibility of null values, if you do not find all three nodes. That means that when you process the array you should check for null anyway. An alternative is to default the array values to something like the empty String:
Stavros Kanarakis wrote:How stupid of me....Of course !!
Thanks Rob, this silly bug kept me busy for two hours now...it is always good to have some fresh eyes look at the code.
That happens to the best of us. Which of course is me
OK, I'm not the best of us, but I too have had cases where I couldn't find the solution, someone else looks at it for a few seconds and finds the solution. And be honest, who hasn't had this happen to him/her?