0

Ok, I cannot figure this out.

I have a method that returns a String array;

public String[] getstatusSelectedFromFragments(){
 int jobCounter = 0;
 String[] podSelectedLines = new String[fragArray.length]; //length will be 1 for testing

 for(PodStatusFragment f : fragArray){
  System.out.println("Job " + (jobCounter+1) + " ;;; ");
  String [] selects = f.getSelections();
  for(String s : selects){ //there are 6 to go through
   podSelectedLines[jobCounter] += s.substring(0, Globals.getPodCodeLen());
   System.out.println(s.substring(0, Globals.getPodCodeLen()));
  }
 jobCounter++;
 }
 return podSelectedLines; //this should contain 6 repeated codes
}

If I System.out the above as it is being populated, it is correct and return the result; "EPODEPODEPODEPODEPODEPOD"

If I then output the contents after they are returned (below), I get the result; "nullEPODEPODEPODEPODEPODEPOD"

int tryer = 0;
String[] testStats = getstatusSelectedFromFragments();
while(tryer<testStats.length){ //length is 1 for testing
 System.out.print("TESTINSTAT::: " + testStats[tryer]);
 tryer++;
}

there is nothing in between, it is only the return of the method being output, but at that point it differs. How am I getting an extra entry, and how is it appearing at the beginning? Am I missing something obvious?

matty357
  • 637
  • 4
  • 16

3 Answers3

1
for (PodStatusFragment f : fragArray) {
    podSelectedLines[jobCounter] = ""; // Was null.
    System.out.println("Job " + (jobCounter+1) + " ;;; ");
    String [] selects = f.getSelections();
    for (String s : selects) { //there are 6 to go through
        podSelectedLines[jobCounter] += s.substring(0, Globals.getPodCodeLen());
        System.out.println(s.substring(0, Globals.getPodCodeLen()));
    }
    jobCounter++;
}

The array is initialized to nulls. Or better use the faster StringBuilder.

for (PodStatusFragment f : fragArray) {
    StringBuilder status = new StringBuilder();
    System.out.println("Job " + (jobCounter+1) + " ;;; ");
    String [] selects = f.getSelections();
    for (String s : selects) { //there are 6 to go through
        status.append(s.substring(0, Globals.getPodCodeLen()));
    }
    podSelectedLines[jobCounter] = status.toString();
    jobCounter++;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Thank you so much, this has worked. How come String [] selects = f.getSelections(); for (String s : selects) { //there are 6 to go through podSelectedLines[jobCounter] += s.substring(0, Globals.getPodCodeLen()); is not setting the first entry? – matty357 Apr 23 '15 at 14:46
  • Can you clarify? in "nullE..." there are 6 "EPOD"s and I should expect only "null" to be dropped. As the String in the list was null and got added to, `null + "EPOD"`yields `"nullEPOD"`. – Joop Eggen Apr 23 '15 at 14:53
1

This line prepares the array:

String[] podSelectedLines = new String[fragArray.length];

What it creates is an array of references to String. Since this is just the array and no content has been entered as yet, each element is actually null. That's the default value when you haven't assigned anything to the array yet.

Then in your code, you do this:

podSelectedLines[jobCounter] += s.substring(0, Globals.getPodCodeLen());

This means "take the current value of the array element and concatenate the value of this string to it". The current value is null. String concatenation translates this to the string "null" and then concatenates the substring to it. So it is equivalent to:

podSelectedLines[jobCounter] = "null" + s.substring(0, Globals.getPodCodeLen());

What you print here:

System.out.println(s.substring(0, Globals.getPodCodeLen()));

is just the part you concatenated to the string, not the actual result.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
0

You must know that if you do something like:

String a = null;
String b = 'test';
System.out.println(a + b);

The result will be 'nulltest'.

However, if I change it to:

String a = "";
String b = 'test';
System.out.println(a + b);

The result will be 'test'.

See Why i'm getting a null string when i try to add the splitted string? and Concatenating null strings in Java

Community
  • 1
  • 1
JFPicard
  • 5,029
  • 3
  • 19
  • 43