c#对象引用未设置为对象的实例. [英] c# Object reference not set to an instance of an object.

查看:161
本文介绍了c#对象引用未设置为对象的实例.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在编辑"视图中运行此代码,但工作正常,但在创建"视图中,它生成的对象引用未设置为此特定行的对象错误的实例

Hi,

I''m running this code in the Edit View and it works fine however in the Create View it generate Object reference not set to an instance of an object error for this specific line

foreach(var evnt in s)



我看不出要抓住这个问题.



I can''t seen to grasp the issue.

<div class="form-group">
           <div class="col-md-offset-2 col-md-10">
               <table>
                   <tr>
                       @{
                                    int cnt = 0;
                       List<GestionSinistresA.ViewModels.StatutFSEvenement>
                           evnts = ViewBag.Evenements;
                           foreach(var evnt in evnts)
                           {
                               if(cnt++ %3 == 0)
                               {
                                   @:</tr><tr>

                               }

                               @:<td>
                                   <input type="checkbox"
                                          name="sinistreEvents"
                                          value="@evnt.EvenementID"
                                          @(Html.Raw(evnt.designee ? "checked=\"checked\"" : "")) />
                                   @evnt.CodeEvenement
                               @:</td>
                               }

                           @:</tr>
                   }
               </table>
           </div>

       </div>




谢谢




Thanks

推荐答案

如果您正确指出了引发异常的行,则s为null.如果使用调试器,您会很快发现错误所在.确实,您不能每次都问这样的问题.您真的需要学会独自应对这种情况.

不要担心.这是最容易检测和修复的情况之一.它仅表示使用和实例(非静态)成员对某个引用类型的某个成员/变量进行了取消引用,这要求该成员/变量为非null,但实际上它似乎为null.只需在调试器下执行它,它将在引发异常的位置停止执行.在该行上放置一个断点,重新启动应用程序,然后再次到达该点.评估下一行中涉及的所有引用,看看哪一个为空,而不必为空.在弄清楚这一点之后,请修复代码:确保将成员/变量正确初始化为非null引用,或者检查它是否为null,如果为null,则执行其他操作.

另请参见:要在单击按钮时显示下一条记录.但在下一个记录功能对象引用未设置为对象的实例"的条件下出现错误.

有时,由于一种或另一种原因,您无法在调试器中执行此操作.一个真正令人讨厌的情况是,只有在没有调试信息的情况下构建软件时,问题才显现出来.在这种情况下,您必须使用更困难的方法.首先,您需要确保您不要通过静默处理异常来阻止异常的传播(这是开发人员针对自己的罪行,但这很常见).您需要在每个线程的最上层堆栈框架上捕获绝对的所有异常.如果处理类型为System.Exception的异常,则可以执行此操作.在处理程序中,您需要记录所有异常信息,尤其是System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx
http://msdn.microsoft.com/en-us/library/system.exception. stacktrace.aspx .

堆栈跟踪只是一个字符串,显示了从throw语句到处理程序的异常传播的完整路径.通过阅读它,您始终可以找到目标.对于日志记录,最好(在大多数情况下)使用类System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics. eventlog.aspx .

祝你好运,
—SA
If your correctly pointed out the line where the exception is thrown, s is null. You could see what''s wrong in not time if you used the debugger. Really, you cannot ask such questions each time it happens; you really need to learn to cope with such situations on your own.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it''s the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA


这篇关于c#对象引用未设置为对象的实例.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆