1

In the image below you can see that running the application in debug mode I get the actual variable that was null which caused the error (circled).

In the catch of this, I looked thru the entire structure of Exception object and did not find the same information.

Does anyone have any suggestions on how to get this same information when not in debug mode?

Full Error Message

Thanks!

LeeFranke
  • 196
  • 1
  • 9

1 Answers1

0

The NullReferenceException error is easy to implement. Often it is because the object is not initialized. But there are many other reasons too.

You can check the following examples, or refer to this link:What is a NullReferenceException, and how do I fix it?.

using System;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                object o = null;
                int d = (int)o;
                Console.WriteLine(d);
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine("ex.Message:" + ex.Message);
                Console.WriteLine("ex.StackTrace:" + ex.StackTrace);
                Console.ReadLine();
            }
        }
    }
}

Uncheck "Break when...", and you can get the information directly in the catch (if you want to check it again, you can add a breakpoint to the error line, etc.).

enter image description here

Eliminating this error basically requires checking that the error object is initialized or not null.

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21
  • Catching the error is fine. If I get the error that means there is some important data that is missing. I catch the error and display the error accordingly. What would be best is if I could get the actual variable that is null (like in the screenshot) and display it. – LeeFranke Mar 31 '23 at 14:52
  • Your question is about how to manifest this error. Can't you output your error using the same method? Is it possible to add an example? – Jiale Xue - MSFT Apr 03 '23 at 09:49
  • To answer your question: No I cannot output the error I'm looking for using the same method because the Exception object does not have the error message 'Customer_Part_Number__c.get returned null'. Yes I know it is a bit confusing that the Exception object is missing part of the error message. – LeeFranke Apr 04 '23 at 15:09