Type mismatch: cannot convert from ArrayList<String> to List<IMendixObject>

“Hi, Here I’m reading one file to take specific data from it. But there are many outputs so I’m using list as return type for java action. While returning list it is giving error: ’Type mismatch: cannot convert from ArrayList<String> to List<IMendixObject> ’ if anyone has any idea about it please let me know ”     package myfirstmodule.actions; import com.mendix.core.Core; import com.mendix.systemwideinterfaces.core.IContext; import com.mendix.webui.CustomJavaAction; import com.mendix.systemwideinterfaces.core.IMendixObject; import java.util.*; import java.util.stream.Collectors; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class GetAllErrorMessages extends CustomJavaAction<java.util.List<IMendixObject>> {     private IMendixObject __inputFile;     private system.proxies.FileDocument inputFile;     public GetAllErrorMessages(IContext context, IMendixObject inputFile)     {         super(context);         this.__inputFile = inputFile;     }     @java.lang.Override     public java.util.List<IMendixObject> executeAction() throws Exception     {         this.inputFile = __inputFile == null ? null : system.proxies.FileDocument.initialize(getContext(), __inputFile);         // BEGIN USER CODE                                             ArrayList<String>             listAllErrorMessagesFromFile = new ArrayList<>(                 Arrays                     .asList());            ArrayList<String>             listAllErrorMessages = new ArrayList<>();                                                    InputStream fstream = Core.getFileDocumentContent(getContext(), inputFile.getMendixObject());                 BufferedReader br = new BufferedReader(new InputStreamReader(fstream));                 Boolean flag= false;                 StringBuffer sb = new StringBuffer();                 String strLine;                 while ((strLine = br.readLine()) != null){                     if (strLine.contains("Failed") && !strLine.isEmpty()) {                         strLine= strLine.replaceAll("[+]","");                         strLine= strLine.replaceAll("time=\"\\d*:\\d*:\\d*.\\d*-\\d*\"","");                         strLine= strLine.replaceAll("time=\"\\d*:\\d*:\\d*.\\d*\"","");                         //strLine= strLine.replaceAll("\\d*+\\d*","");                         strLine= strLine.replaceAll("date=\"\\d*-\\d*-\\d*\"","");                         strLine= strLine.replaceAll("component=\"\\w*\"","");                         strLine= strLine.replaceAll("context=\"\"","");                         strLine= strLine.replaceAll("type=\"\\d{1}\"","");                         strLine= strLine.replaceAll("thread=\"\\d*\"","");                         strLine= strLine.replaceAll("file=\"\\D*\\d*\"","");                         strLine= strLine.replaceAll("LOG","");                         strLine= strLine.replaceAll("[!]*","").replaceAll("\\[","").replaceAll("\\]","\n").replaceAll("[><]","");                         strLine= strLine.replaceAll("found","");                         strLine= strLine.replaceAll("file=\"ZTIDiskpart_\\w*_\\w*\\d*_\\d*\"","");                                                                           listAllErrorMessages.add(strLine);                                              }                 }                                  // Create a new ArrayList                 ArrayList<String> listUniqueErrorMessages = new ArrayList<String>();                                    // Traverse through the first listAllErrorMessages                 for (String element : listAllErrorMessages) {                                    // If this element is not present in listUniqueErrorMessages                 // then add it                 if (!listUniqueErrorMessages.contains(element)) {                                             listUniqueErrorMessages.add(element);                   }                 }                                                                                                      return (listUniqueErrorMessage);                           // END USER CODE     }     /**      * Returns a string representation of this action      */     @java.lang.Override     public java.lang.String toString()     {         return "GetAllErrorMessages";     }     // BEGIN EXTRA CODE     // END EXTRA CODE }
1 answers

You are trying to return a List of Strings, but your Java Action is expecting to return a List of IMendixObject.

When you created your Java Action you said what type of Object it should return. So, in your Java Action you need to create a List (probably an ArrayList) of IMendixObjects. You use the Core.instantiate method to create a concrete object of the correct type, set the values you need in this object, then add to the List. Repeat this until you have all the data you need your List. Finally you return this List.

Hope this helps.


# Update to include example code – this is from the StringSplit java action in Community Commons

List<IMendixObject> returnList = new ArrayList<IMendixObject>();
String[] parts = this.inputString.split(this.splitParameter);
Integer index = 0;
for (String part : parts) {
	IMendixObject splitPart = Core.instantiate(getContext(), SplitItem.getType());
	splitPart.setValue(getContext(), SplitItem.MemberNames.Value.toString(), part);
	splitPart.setValue(getContext(), SplitItem.MemberNames.Index.toString(), index);
	index = index + 1;
return returnList;

This is creating multiple SplitItems using the SplitItem proxy class in CommunityCommons, and then adding them to a List and returning this.