标签: nullreferenceexception

httpcontext.current.server.mappath对象引用未设置为对象的实例

我在一个类中使用以下代码:

string filePath = HttpContext.Current.Server.MapPath("~/email/teste.html");
Run Code Online (Sandbox Code Playgroud)

文件teste.html位于文件夹中

但是当它打开文件时,会生成以下错误:

你调用的对象是空的.

c# nullreferenceexception

36
推荐指数
3
解决办法
5万
查看次数

在构建我的云项目时,"对象引用未设置为对象的实例"

当我使用一堆云项目构建我的解决方案时,我在输出中看到一个或多个"错误:对象引用未设置为对象的实例"消息.当我尝试运行其中一个云项目时,我得到弹出窗口"有构建错误.你想继续并运行上一次成功构建",但错误列表中没有错误和相同的"错误:对象ref ..."输出中的错误.

当我单击我的云项目上的包时,我得到一个消息框,其中"对象引用未设置为对象的实例",当我在云项目中的"角色"下右键单击 - >项目引用的属性时也是如此.

如果我使用命令行使用msbuild构建我的解决方案,我不会收到错误.

我尝试重启Visual Studio以及我的电脑.我还尝试重新安装Azure工具(2.1),然后重新安装Visual Studio.

我的同事现在在Visual Studio 2013 RC上遇到了同样的问题.

有没有人有同样的问题?

我搜索过,但只发现发布时遇到问题的人,解决方案是手动打包.

azure nullreferenceexception visual-studio-2012

35
推荐指数
2
解决办法
3万
查看次数

不可能的NullReferenceException?

我正在研究一位同事在通过Visual Studio 2010运行应用程序时遇到的异常:

System.NullReferenceException was unhandled by user code
  Message=Object reference not set to an instance of an object.
  Source=mscorlib
  StackTrace:
       at System.Collections.Generic.GenericEqualityComparer`1.Equals(T x, T y)
       at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue& value)
       at xxxxxxx.xxxxxxx.xxxxxxx.RepositoryBase`2.GetFromCache(TIdentity id) 
Run Code Online (Sandbox Code Playgroud)

使用.NET Reflector,我查看了代码
GenericEqualityComparer<T>.Equals(T x, T y),我看不出任何可能的原因NullReferenceException.

//GenericEqualityComparer<T>.Equals(T x, T y) from mscorlib 4.0.30319.269
public override bool Equals(T x, T y)
{
    if (x != null)
    {
        return ((y != null) && x.Equals(y));
    }
    if (y != null)
    {
        return false;
    } …
Run Code Online (Sandbox Code Playgroud)

c# mscorlib nullreferenceexception

32
推荐指数
1
解决办法
2901
查看次数

空检查何时可以抛出 NullReferenceException

我知道一开始这似乎是不可能的,一开始对我来说也是如此,但最近我看到了这种代码抛出 a NullReferenceException,所以这绝对是可能的。

不幸的是,Google 上几乎没有任何结果可以解释诸如此类的代码何时foo == null会引发 NRE,这会使调试和理解其发生的原因变得困难。因此,为了记录这种看似奇怪的事件可能发生的可能方式。

这段代码可以以什么方式foo == null抛出NullReferenceException?

c# null nullreferenceexception

32
推荐指数
4
解决办法
2581
查看次数

在DataGridView中将AutoSizeMode设置为AllCells时出现NullReferenceException

我手动将实体框架代码第一个表绑定到datagridview.当我将AutoSizeMode设置为AllCells并向表中添加实例时,我在Add期间得到NullReferenceException.

代码运行如下:

dbContext.Persons.Load();
myDataGridView.DataSource = dbContext.Persons.Local.ToBindingList();

myDataGridView.Columns[ "Description" ].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

Person p = new Person();
p.Name = "Tester Alfred";
p.Description = "Description"; //no more properties, only those two (Id Property is annotated as [Key]

dbContext.Persons.Add( p ); // this throws a NullReferenceException
Run Code Online (Sandbox Code Playgroud)

以下是堆栈跟踪的相关部分:

System.Data.Entity.Core.Objects.ObjectContext.AddSingleObject(EntitySet entitySet, IEntityWrapper wrappedEntity, String argumentName)
   bei System.Data.Entity.Core.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
   bei System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClassd.<Add>b__c()
   bei System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
   bei System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
   bei System.Data.Entity.DbSet`1.Add(TEntity entity)
Run Code Online (Sandbox Code Playgroud)

表人员否则是空的.当我删除AutoSize - 指令时一切都很好.

Plattform:使用Studio 2013的.Net 4.5.1中的WInForms; 运行Win8 Pro,EF …

c# entity-framework datagridview nullreferenceexception winforms

30
推荐指数
1
解决办法
963
查看次数

如何在包中的文件中使用全局var?

我有以下文件结构:

车型/ db.go

type DB struct {
    *sql.DB
}

var db *DB

func init() {
    dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
        DB_USER, DB_PASSWORD, DB_NAME)

    db, err := NewDB(dbinfo)
    checkErr(err)

    rows, err := db.Query("SELECT * FROM profile")
    checkErr(err)

    fmt.Println(rows)
}

func NewDB(dataSourceName string) (*DB, error) {
    db, err := sql.Open("postgres", dataSourceName)
    if err != nil {
        return nil, err
    }
    if err = db.Ping(); err != nil {
        return nil, err
    }
    return &DB{db}, nil
}
Run Code Online (Sandbox Code Playgroud)

车型/ db_util.go

func (p *Profile) …
Run Code Online (Sandbox Code Playgroud)

pointers database-connection go nullreferenceexception

30
推荐指数
3
解决办法
5万
查看次数

为什么实体框架返回空List <>而不是空?

我是ASP .NET MVC世界的新手.也许,这就是我无法向自己解释对我来说是一个令人讨厌的问题的原因.

我有一节有一对多的关系.

class MyClass{
    public List<OtherClass> otherClasses {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

当我持久化这个类的一个实例时,我用空List <>填充它的关系

MyClass myClass = new MyClass(){ otherClasses = new List<OtherClass>() }
context.myClass.Add(myClass);
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试检索该实例时,出于任何原因,我尝试访问该列表,系统给我一个空参考例外...

我的问题是:为什么EF不返回空列表而不是空列表?特别是在这种情况下,我坚持使用空列表?

有什么方法可以避免验证实例是否为空?

c# entity-framework nullreferenceexception ef-code-first entity-framework-4.1

29
推荐指数
2
解决办法
2万
查看次数

什么时候可以为空的类型抛出异常?

请考虑以下代码:

int? x = null;
Console.Write ("Hashcode: ");
Console.WriteLine(x.GetHashCode());
Console.Write("Type: ");
Console.WriteLine(x.GetType());
Run Code Online (Sandbox Code Playgroud)

执行时,它会写入Hashcode 0,但NullReferenceException在尝试确定类型时失败x.我知道调用可空类型的方法实际上是在底层值上调用的,所以我希望程序在期间失败x.GetHashCode().

那么,这两种方法之间的根本区别是什么,为什么第一种方法失败呢?

.net c# nullable nullreferenceexception

29
推荐指数
3
解决办法
2370
查看次数

一个只读静态字段如何为空?

所以这是我的一个课程的摘录:

    [ThreadStatic]
    readonly static private AccountManager _instance = new AccountManager();

    private AccountManager()
    {
    }

    static public AccountManager Instance
    {
        get { return _instance; }
    }
Run Code Online (Sandbox Code Playgroud)

如您所见,它是一个单线程每线程 - 即实例标有ThreadStatic属性.该实例也被实例化为静态构造的一部分.

既然如此,当我尝试使用Instance属性时,我的ASP.NET MVC应用程序中是否有可能出现NullReferenceException?

c# asp.net-mvc static readonly nullreferenceexception

27
推荐指数
3
解决办法
3013
查看次数

在VBScript中检查NULL时出错

我在Classic ASP页面中有以下VBScript:

function getMagicLink(fromWhere, provider)
    dim url 
    url = "magic.asp?fromwhere=" & fromWhere
    If Not provider is Nothing Then ' Error occurs here
        url = url & "&provider=" & provider 
    End if
    getMagicLink = "<a target='_blank' href='" & url & "'>" & number & "</a>"
end function
Run Code Online (Sandbox Code Playgroud)

我不断在行上写一个"Object Required"错误消息If Not provider Is Nothing Then.

值为NULL,或者它不是NULL,为什么我会收到此错误?

编辑:当我调用对象时,我传入NULL,或者传入一个字符串.

vbscript null nothing asp-classic nullreferenceexception

27
推荐指数
2
解决办法
12万
查看次数