标签: nullreferenceexception

在WCF服务方法中使用JSON

在一个较大的项目中,我无法获得WCF服务方法来使用JSON参数.所以我制作了一个较小的测试用例,并且行为得到了回应.如果我调试服务,我可以看到服务调用时参数值为null.Fiddler确认正在发送JSON,JsonLint确认它是有效的.

下面的代码带有调试注释.

[ServiceContract]
public interface IWCFService
{

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "getstring")]

    string GetString();

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "getplayer")]
    //[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest,
    //    ResponseFormat = WebMessageFormat.Json,
    //    UriTemplate = "getplayers")]
    Player GetPlayer();

    [OperationContract]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "getplayers")]
    List<Player> GetPlayers();

    [OperationContract]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        UriTemplate = "totalscore")]
    string TotalScore(Player player);

}
Run Code Online (Sandbox Code Playgroud)

......及其实施

public class WCFService : …
Run Code Online (Sandbox Code Playgroud)

javascript wcf jquery json nullreferenceexception

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

如何修复Orchard CMS中的"对象引用未设置为对象的实例"错误

我正在建立一个果园CMS网站.最初,我下载了压缩版本,但是VS在项目Markdown.csproj第1行中抱怨错误:"对象引用未设置为对象的实例".这对我没有意义,所以我尝试从Web安装它平台安装程序 这有点好用了一段时间.然后我在一些主题更改后重新启动visual studio并再次得到原始错误.我试图恢复主题更改并尝试在管理模式下重新启动VS. 仍然没有骰子.

我可以通过谷歌找到Markdown.csproj的主题.如果有人有建议,我会全力以赴.谢谢.

nullreferenceexception orchardcms

8
推荐指数
1
解决办法
1万
查看次数

C#中的面试问题

技术负责人问我以下内容:
他创建了一个类,声明了一个对象并对其进行了初始化.但在某些情况下,我们可能会得到"空引用"异常.
他评论说,这种例外有1000种可能的原因,并让我猜出一个原因.
我无法弄清楚.什么是原因,我们可能会得到这样的例外?

c# nullreferenceexception

7
推荐指数
6
解决办法
1314
查看次数

MySQL与实体框架 - 我做错了什么?

我对Entity Framework甚至ADO.NET都是全新的(通常不会对数据库做太多工作).

  1. 我下载并安装了MySQL Connector/NET 6.3.5.
  2. 我在Visual Studio 2010中创建了一个新的C#项目.
  3. 我在项目中添加了一个新的ADO.NET实体数据模型,并选择了"从数据库生成".
  4. 我添加了一个新的连接到我的本地MySQL服务器w /服务器名称"localhost"+我的用户名和密码.
  5. 我检查了MySQL数据库中的所有表以生成对象.
  6. 我写了以下代码:

(things这只是一个虚假的表格,我和一些任意的字段一起扔了.)

TestDataEntities entities = new TestDataEntities();

var things = entities.things.Execute(MergeOption.AppendOnly); // exception
Run Code Online (Sandbox Code Playgroud)

上面扔了一个NullReferenceException,我真的很无知为什么会这样.当我测试连接时,它说它成功了.我肯定使用正确的用户名和密码建立连接.我甚至不知道还有什么可以调查.

对于那些真正了解这些东西的人来说,这是我得到的例外(但是,对于我未经训练的眼睛,这实际上看起来并没有那么有用):

   at MySql.Data.MySqlClient.MySqlClientFactory.get_MySqlDbProviderServicesInstance()
   at MySql.Data.MySqlClient.MySqlClientFactory.System.IServiceProvider.GetService(Type serviceType)
   at System.Data.Common.DbProviderServices.GetProviderServices(DbProviderFactory factory)
   at System.Data.Metadata.Edm.StoreItemCollection.Loader.InitializeProviderManifest(Action`3 addError)
   at System.Data.Metadata.Edm.StoreItemCollection.Loader.OnProviderManifestTokenNotification(String token, Action`3 addError)
   at System.Data.EntityModel.SchemaObjectModel.Schema.HandleProviderManifestTokenAttribute(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.Schema.HandleAttribute(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.SchemaElement.ParseAttribute(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.SchemaElement.Parse(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.Schema.HandleTopLevelSchemaElement(XmlReader reader)
   at System.Data.EntityModel.SchemaObjectModel.Schema.InternalParse(XmlReader sourceReader, String sourceLocation)
   at System.Data.EntityModel.SchemaObjectModel.Schema.Parse(XmlReader sourceReader, String sourceLocation)
   at System.Data.EntityModel.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption …

.net mysql entity-framework visual-studio-2010 nullreferenceexception

7
推荐指数
2
解决办法
7884
查看次数

显式抛出NullReferenceException的原因?

我正在清理一些遗留代码,并且我找到了显式抛出的方法NullReferenceException(例如:在检查类的某些属性是否为空时,或检查配置时).由于在空引用的情况下CLR抛出了这种类型的异常,对于应用程序显式抛出,这似乎是一个非常糟糕的异常选择.

我的问题是 - 对于从代码中明确抛出的异常,有什么理由NullReferenceException可以作为一个很好的选择吗?

.net nullreferenceexception

7
推荐指数
2
解决办法
455
查看次数

为什么将ComboBox.SelectedValue设置为null会导致ArgumentNullException?

为什么设置SelectedValueComboBox,以null引起ArgumentNullException

仅当ComboBox实际上是表单的一部分时才会发生异常.我可以设置SelectedValue各种没有意义的值或类型,但我无法设置它null.

SelectedValue不是不可能的null.事实上,它的价值 null在我试图将其设置为时null.

在我的真实代码中,这不会发生在构造函数中,而且我并没有明确地将其设置为null.代码正在使用恰好是的变量null.我可以通过null在尝试设置之前检查变量来修复它SelectedValue.但我不明白的是为什么我不能把它设置为一个null值.

代码编辑:DataSource现在包含ValueMembers值实际的项目null

using System.Collections.Generic;
using System.Windows.Forms;

public class Form1 : Form {
    public Form1() {
        var comboBox1 = new ComboBox();
        Controls.Add(comboBox1);
        comboBox1.ValueMember = "Key";
        comboBox1.DisplayMember = "Value";
        comboBox1.DataSource = new List<Record> {
            new Record {Key = "1", Value = "One"}, 
            new Record …
Run Code Online (Sandbox Code Playgroud)

c# combobox datasource nullreferenceexception winforms

7
推荐指数
2
解决办法
7849
查看次数

F#与C#类互操作,它具有可选的可为空参数设置为null但是null导致NullReferenceException/AccessViolationException

我有以下C#类

public class BadClass
{
    public BadClass(int? bad = 1)
    {
    }
}

public class GoodClass
{
    public GoodClass(int? good = null)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它们都具有可选的可空参数作为其构造函数的一部分,唯一的区别是BadClass将参数default设置为null以外的其他值.

如果我尝试在F#中创建这些类的实例,这就是我得到的:

这很好用:

let g = GoodClass()
Run Code Online (Sandbox Code Playgroud)

这会抛出NullReferenceException:

let b = BadClass()
Run Code Online (Sandbox Code Playgroud)

这会引发AccessViolationException

let asyncB = async { return BadClass() } |> Async.RunSynchronously
Run Code Online (Sandbox Code Playgroud)

知道为什么会这样吗?

编辑

使用ILSpy对其进行反编译,这是F#的输出

C#类位于一个名为InteopTest的程序集中[原文如此]

ILSpy到C#

GoodClass g = new GoodClass(null);
    BadClass b = new BadClass(1);
    FSharpAsyncBuilder defaultAsyncBuilder = ExtraTopLevelOperators.DefaultAsyncBuilder;
    FSharpAsync<BadClass> fSharpAsync = defaultAsyncBuilder.Delay<BadClass>(new Program.asyncB@10(defaultAsyncBuilder));
    FSharpAsync<BadClass> computation = fSharpAsync;
    BadClass asyncB = FSharpAsync.RunSynchronously<BadClass>(computation, …
Run Code Online (Sandbox Code Playgroud)

c# f# interop access-violation nullreferenceexception

7
推荐指数
1
解决办法
1040
查看次数

MVC5 / C# - 无法对空引用执行运行时绑定

我试图找出导致Cannot perform runtime binding on a null reference此代码错误的原因:

var query = "SELECT Id, UserName, List_Order, LoggedIn " + 
            "FROM AspNetUsers" +
            "WHERE LoggedIn = 1" + 
            "ORDER BY List_Order ASC";

var conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
var cmd = new SqlCommand(query, conn);   
conn.Open();
var rdr = cmd.ExecuteReader();
var n = 0;
while(rdr.Read())
{
    if (Convert.ToString(rdr["UserName"]) != null)
    {
        ViewBag.speakers[n] = new string[4] {
            Convert.ToString(rdr["Id"]),
            Convert.ToString(rdr["UserName"]),
            Convert.ToString(rdr["List_Order"]),
            Convert.ToString(rdr["LoggedIn"]) 
        };

        //Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot 
        //perform runtime binding on a null reference …
Run Code Online (Sandbox Code Playgroud)

c# runtime-error exception nullreferenceexception asp.net-mvc-5

7
推荐指数
1
解决办法
4万
查看次数

任何ToList()的EF6 NullReferenceException

使用C#,.NET 4.5.2,Entity Framework 6.1.3和System.Linq我遇到了一个令人困惑的异常.异常本身似乎没有包含有用的信息来确定它被提出的原因.

执行时的以下代码行导致NullReferenceException: dbCtx.Customers.ToList();

但是,以下行无例外地运行并返回正确的结果: (dbCtx.Customers).ToList();

首先运行括号括起的表达式将允许两个表单无异常地执行:

var result1 = (dbCtx.Customers).ToList();
var result2 = dbCtx.Customers.ToList();
Run Code Online (Sandbox Code Playgroud)

我还要注意添加实体按预期工作:

dbCtx.Customers.Add(new Customer() { Enabled = true, Name = "Test" });
Run Code Online (Sandbox Code Playgroud)

客户实体类:

public sealed class Customer : BaseEntity
{
    public bool Enabled { get; set; }

    [Required]
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

BaseEntity类:

public abstract class BaseEntity
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

DbContext类:

public class MyDbContext : DbContext
{
    public MyDbContext() : base(@"Server=.\SQLExpress;Database=MyDatabase;Trusted_Connection=Yes;")
    { …
Run Code Online (Sandbox Code Playgroud)

c# nullreferenceexception entity-framework-6

7
推荐指数
1
解决办法
836
查看次数

等待空传播 System.NullReferenceException

我有以下代码:

await _user?.DisposeAsync();
Run Code Online (Sandbox Code Playgroud)

Visual Studio 突出显示此代码,说“可能的 NullReferenceException”

顺便说一句,没有await Visual Studio不显示此警告为什么NullReferenceException在这里可能?

c# exception nullreferenceexception async-await null-propagation-operator

7
推荐指数
1
解决办法
775
查看次数