简体   繁体   English

如何在发生对象引用的地方打印行号

[英]How to print line number where object reference occurred

I need a suggestion regarding Object reference issue. 我需要有关对象引用问题的建议。 I got this message ExceptionMessage: Object reference not set to an instance of an object. 我收到此消息ExceptionMessage:对象引用未设置为对象的实例。 (because some null object) I am printing the stack trace when an error occurs: (由于一些空对象)发生错误时,我正在打印堆栈跟踪:

 Exception systemException = lastError.GetBaseException();

By using ex.Message I am able to get the method where exception has occurred but I am not getting any information regarding the line (of that method) where exception occurred. 通过使用ex.Message,我可以获取发生异常的方法,但是我无法获取有关发生异常的(该方法的)行的任何信息。 I need to know at which line object reference has occurred and which object was null when this exception came. 我需要知道发生此异常时在哪个行对象引用发生了,哪个对象为null

You need the actual stacktrace: 您需要实际的堆栈跟踪:

systemException.StackTrace

If you want the line numbers, you will need to do something like this: 如果需要行号,则需要执行以下操作:

StackTrace st = new StackTrace(systemException, true);
StackFrame[] frames = st.GetFrames();

foreach(StackFrame frame in frames)
    frame.GetFileLineNumber();

That line number is pretty much guaranteed to be in the stack trace. 该行号几乎可以保证在堆栈跟踪中。 If you want to drill down on it, put a try-catch in the large block of code where it might be happening and drop a breakpoint on the catch block, which will allow you to drop down through the InnerExceptions inside of visual studio (if any exist). 如果要对其进行深入研究,请将try-catch放在可能发生的大代码块中,然后在catch块上放置一个断点,这将使您可以在Visual Studio内部通过InnerExceptions进行下拉(如果任何存在)。

Maybe this helps 也许这有帮助

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string a = null;
                a.ToString();
            }
            catch (Exception ex)
            {
                var s = ex.StackTrace;
                Console.WriteLine(ex.Source);
                int st = s.LastIndexOf("line");
                Console.WriteLine(s.Substring(st, s.Length - st));
            }
        }
    }

Edited : 编辑:

And yes I just learned, using StrackTrace in the answer posted by EkoostikMartin. 是的,我刚刚在EkoostikMartin发布的答案中使用StrackTrace进行了学习。 I think it is a better answer. 我认为这是一个更好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取ASP.NET Core中发生异常的行号 - Get line number where exception occurred in ASP.NET Core 没有行号和类别的错误:“对象引用未设置为对象的实例。” - Error with no line number and class: “Object reference not set to an instance of an object.” 确定堆上对象的对象分配发生的位置 - Determining where object allocations for objects on the heap occurred 对象引用未设置为对象的实例:堆栈跟踪显示错误的行号 - object reference not set to an instance of an object: Stack Trace showing wrong line number 如何计算行数并在结果前面的每行上打印行号? - How to count the number of lines and print the line number on each line in front of the result? 在何处/如何实例化对象以处理空引用异常 - Where/How to instaniate an object to handle null reference exception 如何获取出现该单词的每个单词的行数(非数量) - How to get number of line(not ammount) for each word where this word occured 如何在匹配帐号时编辑文本中的一行 - How to edit a line in text while where matches account number 使用c#如何打印从解析文件中随机选择的行的行号 - using c# how to Print Line number of randomly selected line from parsed file C#where子句按引用复制对象 - C# where clause copy object by reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM