My Java class returns an ArrayList<String> but will not populate my WEBMethods StringList, why?

StackOverflow https://stackoverflow.com/questions/21467251

  •  05-10-2022
  •  | 
  •  

Question

I am trying to populate a WEBMethods String List with an ArrayList and the variable is not populated and no error in WEBMethods occurs. Why is it that my String List is not populating.

Question: String List not populated with ArrayList returned from Java?

Code:

   This is setting no value to the String List  -->

   listOfFileName = FindArrayListOfFiles.getMyFileList();

The getMyFileList() is not setting the value of the ArrayList to the String List in the Java Flow Service. There is no error on the webmethods other than my values are not int he String List when I open it and see.

enter image description here

There is nothing located in my list and I am not certain why that is the case.

enter image description here

Was it helpful?

Solution

In the code in your original unedited question, no elements are ever added to the java.util.ArrayList object.

java.util.List<String> list = new java.util.ArrayList<String>();
Object[] array = list.toArray(); // array.length = 0

IDataCursor cursor = pipeline.getCursor();
IDataUtil.put(cursor, "array", array);
IDataUtil.put(cursor, "array.length", array.length);
cursor.destroy();

Your screenshot of the Service Result tab in Designer actually shows that ListOfFileNames was returned by your service correctly, so that part of your code is working. I know this because if it wasn't returned, then it wouldn't be listed in the Service Result tab at all. The fact that it is listed there means that it was returned correctly.

But the reason why you think it's not working is because ListOfFileNames is a zero-length array, and thus Designer has no value to display in the right column of the Service Result tab. If you had added at least one element to the ArrayList before calling .toArray() on it, then Designer will have something to show you: the resulting array's contents.

The other problem I noticed in the Designer screenshot is that ListOfFileNames is an Object[], rather than a String[]. That's because you're using .toArray() with no arguments, which returns an Object[]. Use the other version, .toArray(T[] a), as follows to make sure you get a String[] returned:

java.util.List<String> list = new java.util.ArrayList<String>();
list.add("item1");
String[] array = list.toArray(new String[0]); // array.length = 1

IDataCursor cursor = pipeline.getCursor();
IDataUtil.put(cursor, "array", array);
IDataUtil.put(cursor, "array.length", array.length);
cursor.destroy();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top