I have read up on it a bit and I understand that in java you can’t change the original value of given parameters and expect those to persist after the method is over. But I’d really like to know a good way to do this. Could someone give me some pointers on what I could do to make this method work? Thanks.
/**
* This will set values in the given array to be "" (or empty strings) if they are null values
*
* @param checkNull
*/
public static void setNullValuesBlank(String... checkNull) {
for (int i = 0; i < checkNull.length; i++) {
String check = checkNull[i];
if (check == null) {
check = "";
}
}
}
EDIT
So I have to set it to the array as several people mentioned, and it works great if I construct the array in the first place, but if I don’t then it doesn’t work.
Here’s the fixed method:
/**
* This will set values in the given array to be "" (or empty strings) if they are null values
*
* @param checkNull
*/
public static void setNullValuesBlank(String... checkNull) {
for (int i = 0; i < checkNull.length; i++) {
if (checkNull[i] == null) {
checkNull[i] = "";
}
}
}
Here’s a call where it works:
String s = null;
String a = null;
String[] arry = new String[]{s, a};
for (int i = 0; i < arry.length; i++) {
System.out.println(i + ": " + arry[i]);
}
setNullValuesBlank(arry);
for (int i = 0; i < arry.length; i++) {
System.out.println(i + ": " + arry[i]);
}
Here’s a call where it doesn’t work, but I want it to:
String q = null;
String x = null;
System.out.println("q: " + q);
System.out.println("x: " + x);
setNullValuesBlank(q, x);
System.out.println("q: " + q);
System.out.println("x: " + x);
Output of that:
q: null
x: null
q: null
x: null
You need to assign to the array:
Assigning to the check will not change the array.