哪个对象到底导致“对象引用未设置到对象的实例”?

发布于 2024-10-04 21:24:08 字数 215 浏览 5 评论 0原文

我有在服务器上运行的 DLL,但由于多种原因无法调试它,我记录了它的异常。
有时,我在一个方法调用中遇到“对象引用未设置为对象的实例”,该方法调用有大约 20 个参数,我需要很长时间才能知道导致异常的对象在哪里。

如果出现“对象引用未设置到对象的实例”以及导致异常的对象名称,是否有任何方法可以记录异常?

I have DLL that run on server and I can't debug it for many reasons, I log its exceptions.
sometimes I have "Object reference not set to an instance of an object" in a method call that have about 20 parameter that take from me long time to know where's the object that cause the exception.

Is there any way to log the exception in case of "Object reference not set to an instance of an object" with the name of the object that cause the exception ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱,才寂寞 2024-10-11 21:24:08

如果您想记录为 null 的确切参数(听起来调试是不可能的),您将需要单独测试每个参数是否为 null 并抛出 ArgumentNullException(如果参数名称为 null)作为字符串传递给异常的构造函数

这是接受 20 个参数的方法可能导致棘手的维护问题的原因之一(也是众多原因之一)。它表明一个方法可能有太多的职责,并且可能试图做超出其应有的事情。当您在编写代码时,您可能需要考虑将此方法分解为更小的部分。

If you want to log the exact argument that was null (it sounds like debugging is out of the question) you will need to test each parameter individually for null and throw an ArgumentNullException for that parameter if it is null with the name of the parameter passed as a string to the exception's constructor.

This is one reason (among many) that a method accepting 20 parameters can lead to tough maintenance issues. It indicates that a method may have too many responsibilities and may be trying to do more than it should. While you are in the code you might want to consider breaking this method up into smaller pieces.

◇流星雨 2024-10-11 21:24:08

如果您知道发生异常的行,则可以使用 Redgate .NET 反汇编程序集Reflector Free 获取更多信息!

否则:不,没有机会获取参数名称,这会导致异常,除非有一个argumentException(其中参数名称包含在异常中)

If you know the line where the exception happens, you could disassemble the assembly with Redgate .NET Reflector Free the get more information!

otherwise: no, there's no chance to get the parameter-name, which caused an exception, except there's an argumentException (where the name of the parameter is included in the exception)

听不够的曲调 2024-10-11 21:24:08

确定导致此问题的对象的最简单方法是将调试器附加到您正在运行的任何代码。您可能需要运行一个单独的服务器,运行在调试模式下构建的代码。在 Visual Studio 中,如果进入“调试”->“异常...菜单项您将看到一个包含两列的异常类型对话框。检查“抛出”列中的公共语言运行时异常。然后尝试抛出异常的操作。这是导致问题的对象的最简单方法。

您可以尝试查看是否存在因出现此错误而引起的任何副作用,并从那里回溯。

您还可以打印错误的整个堆栈跟踪,而不仅仅是错误消息。这可能会为您提供所需的详细信息。

The easiest way to determine what object is causing this is to attach the debugger to whatever code you have running. You probably will need to run a separate server running the code built in debug mode. In Visual Studio if you go into the Debug -> Exceptions... menu item you will have a dialog of exception types with two columns. Check the Thrown column for Common Language Runtime Exceptions. Then try the operation where the exception is being thrown. That's the easiest way what object is causing the problem.

Another thing you can try to see if there are any side affects being caused by this error appearing and back trace it from there.

You also could print the whole stack trace for the error instead of just the error message. That might give you the detail you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文