首页 新闻 赞助 找找看

如何精确定位“Object reference not set to an instance of an object.”错误的出错位置

0
悬赏园豆:200 [已解决问题] 解决于 2011-11-17 20:48

我的程序日志记录代码如下:

protected override void OnException(ExceptionContext filterContext)
{
log4net.Error("【IP】" + filterContext.HttpContext.Request.UserHostAddress + "\r\n\r\n" +
"【Url】" + filterContext.HttpContext.Request.Url.AbsoluteUri + "\r\n\r\n" +
"【Message】" + filterContext.Exception.Message + "\r\n\r\n" +
"【StackTrace】" + filterContext.Exception.StackTrace + "\r\n\r\n");
}

得到的日志信息如下:

【Message】Object reference not set to an instance of an object.

【StackTrace】 at ASP._Page_Views_List_test_cshtml.Execute()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)

这是一个很常见的“Object reference not set to an instance of an object”错误,IP跟URL没帖出来,通过这个日志我仅能知道是哪个页面出错了,但不能精确地知道错误的出错位置,请问有没有办法可以精确地定位。

artwl的主页 artwl | 专家六级 | 园豆:16736
提问于:2011-11-01 11:53
< >
分享
最佳答案
0

VS菜单“DEBUG”->"Exceptions",把CLR Exceptions下的System下的System.NRE钩选成thrown,再debug,这样就会在任何抛出NRE的时候不catch而直接进入调试。

收获园豆:50
水牛刀刀 | 大侠五级 |园豆:6350 | 2011-11-01 13:01

这个方法不行哦,现在的问题是在我机子上怎么运行都没错误,但放到服务器上后偶尔会记录到问题中描述的错误,怎么试都不会进入调试

artwl | 园豆:16736 (专家六级) | 2011-11-01 13:48

@天行健 自强不息: 那这么说有没可能是跟数据相关的?只有服务器上的一些数据导致产生了一个null对象?既然没办法在VS下debug,这种情况我觉得只能在那个页面的代码里把可能抛出NRE的地方都catch起来,然后输出更详细的信息,等下次出这个错的时候再作为依据去检查了。

水牛刀刀 | 园豆:6350 (大侠五级) | 2011-11-01 17:37
其他回答(5)
0

那你从底层开始往上逐层抛异常试试 一直到页面端

收获园豆:30
贺臣 | 园豆:307 (菜鸟二级) | 2011-11-01 12:00
OnException这个方法是个基类,按你的方法太复杂了,代码改动地方太多,万不得以再采取这种方法
支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2011-11-01 12:02
0

ASP._Page_Views_List_test_cshtml 是该变量为null,还是ASP._Page_Views_List_test_cshtml.Execute()函数中的某个变量是null? 你可以实际测试下,看下抛的异常是如何定位的.

收获园豆:40
Launcher | 园豆:45045 (高人七级) | 2011-11-01 12:59

现在的问题是在我机子上怎么运行都没错误,但放到服务器上后偶尔会记录到问题中描述的错误,纠结

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2011-11-01 13:47

@天行健 自强不息: 用VS或WinDG附加到w3wp进程上,有异常时就会在调试器里触发中断.异常应该指示的是_Page_Views_List_test_cshtml 为 null.

支持(0) 反对(0) Launcher | 园豆:45045 (高人七级) | 2011-11-01 17:35
0

这个先帮你顶一下。

收获园豆:10
Eric Sun | 园豆:229 (菜鸟二级) | 2011-11-01 13:29
0

通过ILSPY反编译System.Web.WebPages.WebPageBase的代码(代码如下),然后寻找原因。

// System.Web.WebPages.WebPageBase
public override void ExecutePageHierarchy()
{
if (WebPageHttpHandler.ShouldGenerateSourceHeader(this.Context))
{
try
{
string virtualPath = this.VirtualPath;
if (virtualPath != null)
{
string text = this.Context.Request.MapPath(virtualPath);
if (!text.IsEmpty())
{
base.PageContext.SourceFiles.Add(text);
}
}
}
catch
{
}
}
TemplateStack.Push(this.Context, this);
try
{
this.Execute();
}
finally
{
TemplateStack.Pop(this.Context);
}
}
收获园豆:30
dudu | 园豆:31075 (高人七级) | 2011-11-01 14:04
0

一般碰到这种情况我都是用最笨的办法,给可能出现异常的代码前加上一堆记录日志和关键数据的方法,然后等着下次出错的 时候看日志,不然还真拿这种偶尔出现又无从调试的错误没辙啊

收获园豆:40
七月霄雨 | 园豆:1282 (小虾三级) | 2011-11-02 22:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册