Assign: Object reference not set to an instance of an object error

Hello Everyone,

I’m trying to extract a specific text line inside a txt file. I am using deserialized json activity. when I debug my process, I get error “Assign: Object reference not set to an instance of an object.” on the assign activity. Please see image below:

Hi, @mapaje

The Assign activity error means your JSON object is null or the key “age” does not exist. Ensure the variable from Deserialize JSON is initialized and contains the property by checking
AccessTokenObj IsNot Nothing AndAlso AccessTokenObj(“age”) IsNot Nothing
before assignment

Hello @arjun.shiroya

Would it be okay for your to elaborate that? I’ve checked my variables and confirmed they exist, please see below image:

I tried setting “age” as genericvalue data type (base on the UiPath document available) then converted it to string but I still get the same error so I just returned it back to string

@mapaje

Even if your variable exists and scope is correct, if AccessTokenObj (the JObject output after deserialization) is still empty or does not contain the expected “age” key, you’ll get the same error. Make sure:

The JSON input string actually includes “age” field and is not empty before deserialization.

Use AccessTokenObj IsNot Nothing AndAlso AccessTokenObj.SelectToken(“age”) IsNot Nothing in an If/Assign activity.

If deserialization fails (e.g., JSON is malformed or empty), the output will be blank, and accessing “age” causes the error.

Try logging or displaying the content of AccessTokenObj.ToString right after deserialization to verify it worked and contains “age”
this helps pinpoint if the error is in your data or the extraction method

@arjun.shiroya

I got it, using AccessTokenObj.ToString in the writeline actually displayed the data of my text file.

I was hoping if you could help me as well, I want to use regex to get a specific line on the txt file only. (try to get only the access token) so I can use it to perform API calls

@mapaje

Read the text file into a string, then use this regex in an Assign or Matches activity:
System.Text.RegularExpressions.Regex.Match(strText, “(?<=access_token[":\s]*)[A-Za-z0-9-_]+”).Value
This will extract only the access token if the text contains something like access_token: yourtoken, and you can use the result directly for your API call.

@arjun.shiroya

Thank you very much for the help, I am able now to call the api after extracting the access token using your syntax above.

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