我有这个代码:
using DC = MV6DataContext;
using MV6; // Business Logic Layer
// ...
public DC.MV6DataContext dc = new DC.MV6DataContext(ConnectionString);
IP ip = new IP(Request.UserHostAddress);
dc.IPs.InsertOnSubmit(ip);
dc.SubmitChanges();
// in Business Logic layer:
public class IP : DC.IP {
public IP(string address) { ... }
}
Run Code Online (Sandbox Code Playgroud)
在尝试InsertOnSubmit(ip)时,我得到一个NullReferenceException(对象引用未设置为对象的实例).dc不为空; ip和ip的所有属性都不为null; 虽然有些是空的.
VS2008不会让我进入InsertOnSubmit,因此在评估时我无法知道具体为null.是什么赋予了?
注意:我已经检查过,并且由FK关系创建的所有Linq.EntitySets都存在且非空.
我有一个VS 2010 C#.NET 4项目.问题是程序在调试期间没有破坏'NullReferenceException'错误.
输出窗口将显示以下内容:
A first chance exception of type 'System.NullReferenceException' occurred in myProgram.exe
...但是调试器将退出该函数并继续运行该程序的其余部分.
如何更改此行为以便调试器在这些异常中中断?
c# debugging break visual-studio-2010 nullreferenceexception
我有以下代码,表现出一个奇怪的问题:
var all = new FeatureService().FindAll();
System.Diagnostics.Debug.Assert(all != null, "FindAll must not return null");
System.Diagnostics.Debug.WriteLine(all.ToString()); // throws NullReferenceException
Run Code Online (Sandbox Code Playgroud)
FindAll方法的签名是:
public List<FeatureModel> FindAll()
Run Code Online (Sandbox Code Playgroud)
单步执行代码我已经确认FindAll的返回值不为null,正如您从Assert中看到的那样,"all"变量不为null,但在下一行中它似乎为null.
调用ToString()方法时,问题不是特定于失败.在尝试追踪根本原因时,我将其简化为这个可重复的示例.
这可能是一个线索:在调试器中,变量"all"出现在Locals窗口中,其值为"无法获取本地或参数的值'all',因为它在此指令指针处不可用,可能是因为它已经被优化了."
我考虑尝试其他地方记录的方法之一来禁用代码优化,但这并不能真正解决问题,因为代码的发布版本仍然会被优化.
我在Visual Studio 2010中使用.NET 4.0.
有什么想法吗?
更新:根据请求,这是整个方法:
protected override List<FeatureModel> GetModels() {
var all = new FeatureService().FindAll();
var wr = new WeakReference(all);
System.Diagnostics.Debug.Assert(all != null, "FindAll must not return null");
System.Diagnostics.Debug.WriteLine(wr.IsAlive);
System.Diagnostics.Debug.WriteLine(all.ToString()); // throws NullReferenceException
return all;
}
Run Code Online (Sandbox Code Playgroud)
作为一个仅供参考,原始实施只是:
protected override List<FeatureModel> GetModels() {
return new FeatureService().FindAll();
}
Run Code Online (Sandbox Code Playgroud)
我最初在调用方法中遇到了null异常.我发布的代码是在跟踪问题一段时间之后.
更新#2:根据要求,这是来自异常的堆栈跟踪:
at FeatureCrowd.DomainModel.FeatureSearch.GetModels() in C:\Users\Gary\Documents\Visual Studio …Run Code Online (Sandbox Code Playgroud) 我正在开展一个大型项目,即使有1000次自动测试和100%代码覆盖率,我们也会得到一些荒谬的错误.我们得到的大约95%的错误是NullReferenceExceptions.
有没有办法在编译时强制执行空值检查?
除此之外,有没有办法在单元测试中自动执行空值检查而不必自己编写空案例的测试?
当使用调用SqlCommand.ExecuteReader()方法时,ReSharper告诉我,当我之后使用SqlDataReader对象时,我有一个可能的NullReference异常.
所以使用以下代码:
using (SqlConnection connection = GetConnection())
{
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = ; //snip
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//snip
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该while (reader.Read())行加下划线.
我的问题是读者对象何时会为空?我从来没有遇到它,文档没有提到它可能.我应该检查它是否为空或是否可以安全忽略?
为什么ReSharper认为它可能为null,例如它让我使用SqlCommand而不建议检查null?我猜是ExecuteReader方法有一个属性.
如何测试通用字典对象以查看它是否为空?我想运行一些代码如下:
while (reportGraphs.MoveNext())
{
reportGraph = (ReportGraph)reportGraphs.Current.Value;
report.ContainsGraphs = true;
break;
}
Run Code Online (Sandbox Code Playgroud)
reportGraph对象的类型为System.Collections.Generic.Dictionary当运行此代码时,reportGraphs字典为空,MoveNext()立即抛出NullReferenceException.如果有更高效的处理空集合的方法,我不想在块周围放置try-catch.
谢谢.
看起来NLog不能使用反射GetCurrentClassLogger(),即使我的MVC 3应用程序部署在IIS7上的完全信任环境中.我正在使用StructureMap 2.6.1,并且问题似乎在部署之间偶尔出现.我无法弄清楚为什么,虽然我认为StructureMap没有引起它.
Bootstrapper 类:public static class Bootstrapper
{
public static void ConfigureStructureMap()
{
ObjectFactory.Initialize(Init);
}
private static void Init(IInitializationExpression x)
{
x.AddRegistry(new DBServiceRegistry());
x.AddRegistry(new MyRegistry());
}
}
Run Code Online (Sandbox Code Playgroud)
Registry 类:public class MyRegistry : Registry
{
public MyRegistry()//HttpContext context)
{
For<ILogger>().Use<NLogLogger>();
For<IUserRepository>().Use<SqlUserRepository>();
}
}
Run Code Online (Sandbox Code Playgroud)
我的机器上的一切都很棒.System.NullReferenceException: Object reference not set to an instance of an object部署时为什么会出现错误?
[NullReferenceException: Object reference not set to an instance of an object.]
NLog.LogManager.GetCurrentClassLogger() +84
lambda_method(Closure , IArguments ) …Run Code Online (Sandbox Code Playgroud) structuremap nlog full-trust nullreferenceexception asp.net-mvc-3
AccountController.cs安排AccountController上课.Login该类的方法失败了.特别,
var result = await SignInManager.PasswordSignInAsync(
model.Email, model.Password, model.RememberMe, shouldLockout: true);
Run Code Online (Sandbox Code Playgroud)
在Login方法内投掷System.NullReferenceException.
我已经验证了model.Email,model.Password并且model.RememberMe不是空的.下一步是深入挖掘SignInManager,这是一个AccountController类型的对象ApplicationSignInManager.从上面一行调用的get访问器SignInManager是
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ??
HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
// private set...
}
Run Code Online (Sandbox Code Playgroud)
我改写为
public ApplicationSignInManager SignInManager
{
get
{
var c = HttpContext.GetOwinContext();
var m = c.Get<ApplicationSignInManager>();
return _signInManager ?? m;
}
// private set...
}
Run Code Online (Sandbox Code Playgroud)
调试并确保get访问者没有返回 …
时间再次吸引更多的思想.
我遇到了一个非常奇怪的现象.正如标题所述,我在尝试创建EF ObjectContext时遇到NullReferenceException,但是如果我在Using语句中创建上下文,我只会得到异常.我尝试了各种不同的方式,但总是一样.当然,这是直到昨天才能正常工作的代码.昨天早上我的Windows Update运行可能是相关的.
无论如何...
如果我试试这个
using (var context = new Entities(Env.Instance.Connection))
{
//do a bunch of EF stuff
}
Run Code Online (Sandbox Code Playgroud)
我在创建ObjectContext时得到NullReferenceException.这里的Env.Instance.Connection是在程序的早期创建的EntityConnection.我已经介入以确保实例和EntityConnection都存在.
如果我这样做的话
var context = new Entities(Env.Instance.Connection);
//do a bunch of EF stuff
context.Dispose();
Run Code Online (Sandbox Code Playgroud)
一切正常.
我试过了
using (var context = new Entities(Env.Instance.ConnectionName)) //the name of a connection string in my App.Config
{
//do a bunch of EF stuff
}
Run Code Online (Sandbox Code Playgroud)
我试过了
using (var context = new Entities(Env.Instance.ConnectionString)) //the actual connection string
{
//do a bunch of EF stuff
}
Run Code Online (Sandbox Code Playgroud)
我甚至试过了
using …Run Code Online (Sandbox Code Playgroud) c# entity-framework using-statement nullreferenceexception objectcontext
好吧,几个月前我问了一个关于C和C++的类似问题,但由于整个"Windows Phone"的缘故,我最近一直在关注C#.
那么,在C#中,是否应该在方法边界检查NULL?我认为这与C和C++不同,因为在C#中,通常可以确定给定的引用是否有效 - 编译器将阻止在任何地方传递未初始化的引用,因此唯一剩下的可能错误是它为null .此外,在.NET Framework中为这些事物定义了一个特定的异常,即ArgumentNullException,它似乎编写了程序员认为在传递无效null时应该获得的异常.
我的个人意见再次表明,打电话的人打破了这个问题,而且打电话的人应该把NRE扔到他们面前,直到几天结束.但是,我对这方面的不太确定,而不是我在本地代码领域 - 在这方面,与C或C++相比,C#的编程风格完全不同.
那么......你应该在C#方法中检查空参数吗?
c# ×8
.net ×1
arguments ×1
asp.net ×1
asp.net-mvc ×1
break ×1
collections ×1
debugging ×1
full-trust ×1
generics ×1
linq ×1
linq-to-sql ×1
nlog ×1
null ×1
owin ×1
resharper ×1
sqlcommand ×1
structuremap ×1