Proper way of invoking code

Hello guys, I am trying to invoke a code like this:

if (File.Exists(in_rutaArchivoXML))
{
    // reads .xaml as string
    string solicitudesXamlText = File.ReadAllText(in_rutaArchivoXML);

    // creates new XmlDocument object
    XmlDocument solicitudesXML = new XmlDocument();
    // loads the doc
    solicitudesXML.LoadXml(solicitudesXamlText);

    // list of nodes /descargaSolicitudesEESS/solicitudRecibidaEESS
    XmlNodeList xnList = solicitudesXML.SelectNodes("/descargaSolicitudesEESS/solicitudRecibidaEESS");           

    // iterate through list
    foreach (XmlNode xn in xnList)
    {        
	    string nroOperacion = xn["nroOperacion"].InnerText;
	    out_nrosOper.Add(nroOperacion);           
    }
}

What I am trying to achieve is to read a xml document whose file path is in_rutaArchivoXML (one of my Invoke Code arguments) and obtain a list of values which is out_nrosOper - List<string> (my second argument) but I am getting an error which says "Invoke code: Exception has been thrown by the target of an invocation."

Any hint would be great. Also I share my original C# function code (which gets me what I need using Visual Studio)

    public static List<string>? ObtenerNrsOper(string rutaArchivoXML)
    {
        List<string> nrosOperacion = new List<string>();

        if (File.Exists(rutaArchivoXML))
        {
            // lee .xaml como texto
            string solicitudesXamlText = File.ReadAllText(rutaArchivoXML);

            // crear nuevo objeto XmlDocument
            XmlDocument solicitudesXML = new XmlDocument();
            // carga el documento Xml
            solicitudesXML.LoadXml(solicitudesXamlText);

            // lista de nodos /descargaSolicitudesEESS/solicitudRecibidaEESS
            XmlNodeList? xnList = solicitudesXML.SelectNodes("/descargaSolicitudesEESS/solicitudRecibidaEESS");           

            if (xnList is not null)
            {
                // iterar por lista de nodos
                foreach (XmlNode xn in xnList)
                {
                    if (xn is not null)
                    {
                        string nroOperacion = xn["nroOperacion"].InnerText;
                        nrosOperacion.Add(nroOperacion);
                        Console.WriteLine(nroOperacion);
                    }
                }
            }

            Console.WriteLine("finalizado");

            return nrosOperacion;

            

        }

        return null;


    }

Hi,

For now, can you try to check content of $exceptionDetails at LocalsPanel when error occurs in debug mode? We’ll be able to see detailed information for the exception.

Regards,

Thanks for the response, this is what I get

RemoteException wrapping System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.  ---> RemoteException wrapping System.NullReferenceException: Object reference not set to an instance of an object. 
   at UiPath.CodeRunner.UiPathCodeRunner_ca28e292973140e5a029bdc03ed8f58d.Run(String in_rutaArchivoXML,
List`1& out_nrosOper)
	--- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target,
Object[] arguments,
Signature sig,
Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,
Object[] parameters,
Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,
BindingFlags invokeAttr,
Binder binder,
Object[] parameters,
CultureInfo culture)
   at System.RuntimeType.InvokeMember(String name,
BindingFlags bindingFlags,
Binder binder,
Object target,
Object[] providedArgs,
ParameterModifier[] modifiers,
CultureInfo culture,
String[] namedParams)
   at UiPath.Activities.System.Utilities.InvokeCode.CompilerRunner.Run(Object[] args)
   at UiPath.Activities.System.Utilities.InvokeCode.NetCodeInvoker.Run(String userCode,
List`1 inArgs,
IEnumerable`1 imps,
Object[] args)
   at UiPath.Core.Activities.InvokeCode.Execute(CodeActivityContext context)
   at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance,
ActivityExecutor executor,
BookmarkManager bookmarkManager)
   at System.Activities.ActivityInstance.Execute(ActivityExecutor executor,
BookmarkManager bookmarkManager)
   at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor,
BookmarkManager bookmarkManager,
Location resultLocation)

Hi,

This message shows Object reference not set to an instance.

If out_nrosOper is out type argument, it might be necessary to create instance at the beginning of the code as the following.

out_nrosOper = new List<string>();

Or if you already create instance outside InvokeCode, it might be necessary to set argument type In/Out type.

Regards,

That was it, thanks you @Yoichi !!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.