Read and write range from latest downloaded file ('Object reference not set to an instance of an object')

Hi All,

I’m trying to get UiPath to auto-download a data from website and after which it will go to my ‘downloads’ folder and open up the latest file.
I then want it to read range from that excel file and write on a separate excel file I have.
However when writing range, I get an error of ‘Object reference not set to an instance of an object’. Below is my workflow. Appreciate if I can get help to resolve the error. Thanks!

My FileName = String.Join(“parameter”,Directory.GetFiles(FolderPath,“*”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1))

Can you check if datatable has values using output datatable activity?

Hi @victorialim,

Are you sure the file is downloaded? You are probably trying to read the file before it is fully downloaded.

As a workaround, you can statically add a delay after downloading the file and test it.


What’s with the String.Join? You don’t need that, you aren’t returning an array, you’re returning an individual string element from the array by using the index. You only need the Directory.GetFiles expression.

Also, 1 is the wrong index. It’s a 0 based array so use (0) to get the first file. That’s why you’re getting the error, you’re specifying an array element that doesn’t exist.

Sorry, I misread your expression. The (1) is correct as it’s a Take index, not the index of the Directory.GetFiles array.

Just get rid of the String.Join and use…

Directory.GetFiles(FolderPath,“*”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)

Getting the below error when I removed String.Join

Have added delay but still getting the below error →

RemoteException wrapping System.NullReferenceException: Object reference not set to an instance of an object.
at UiPath.Core.Activities.ScopeActivity.OnFaulted(NativeActivityFaultContext faultContext, Exception propagatedException, ActivityInstance propagatedFrom)
at System.Activities.Runtime.FaultCallbackWrapper.Invoke(NativeActivityFaultContext faultContext, Exception propagatedException, ActivityInstance propagatedFrom)
at System.Activities.Runtime.FaultCallbackWrapper.FaultWorkItem.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)

Can you print using a writeline when you assign the filename? We can check if it really has the correct extension.

Oh, the result of the expression is an array with only one element.

Try this.

Directory.GetFiles(FolderPath,“*”).OrderByDescending(Function(d) New FileInfo(d).CreationTime).Take(1)(0)

Also, you don’t need to assign this to a variable. You can just put it directly into the property of the Excel Application Scope. It’s good to avoid unnecessarily creating variables. Only if you need the filepath/name later should you assign it to a variable. If you’re only using it for the Excel Application Scope, just put the expression into its property and get rid of the FileName variable.

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