标签: nullreferenceexception

不一致的空值引用异常

当我尝试从序列化类对象存储我的值时,我面临不一致的空值引用错误.

if ( item.current_location.city!=null )
{
    var city = item.current_location.city.Select(i => i.ToString());
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码片段中,即使item数组中的任何索引具有空值,也会成功插入.但它在某些情况下抛出异常,我认为不能以任何方式区分其他情况(当值为null时)

c# asp.net nullreferenceexception

-1
推荐指数
1
解决办法
178
查看次数

异常会影响应用程序的性能吗?

我有一个 C# 应用程序..我不断收到空引用异常..我设法捕获此异常并记录它..但我怀疑此异常是否会影响我的应用程序的性能..请注意,我并不想避免异常,相反,我需要知道如果连续触发此异常是否会影响我的应用程序的性能。

c# exception nullreferenceexception

-1
推荐指数
1
解决办法
296
查看次数

基本错误System.NullReferenceException

我有这个基本错误,我无法修复...对象引用未设置为对象的实例

我正在使用asp.net mvc4和ef

我的控制器

 public class PostController : Controller
    {
        private UsersContext db = new UsersContext();

        public ActionResult Index()
        {
            return View(db.Posts.ToList());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection values)
        {
            var post = new Post();
            TryUpdateModel(post);

            if(ModelState.IsValid)
            {
                var context = new UsersContext();
                var username = User.Identity.Name;
                var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
                var userid = user.UserId;
                // var firstname = user.FirstName;

                post.UserId = userid;
                post.Date = DateTime.Now;

                db.Posts.Add(post);
                db.SaveChanges();
            }
            return …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc nullreferenceexception razor

-1
推荐指数
1
解决办法
729
查看次数

未将对象引用设置为访问 DataGridView 的对象的实例

我想将数据从 DataGridView 传输到另一个,这是我的代码示例:

private void btnShow(object sender, EventArgs e)
{
    DataTable dtr = new DataTable();
    dtr.Columns.Add(new DataColumn("Name", typeof(string)));
    dtr.Columns.Add(new DataColumn("Label", typeof(string)));
    dtr.Columns.Add(new DataColumn("Domain", typeof(string)));

    for (int i = 0; i < dataGridView1.Rows.Count; i++)
    {
        DataRow erow = dtr.NewRow();
        erow[0] = dataGridView1.Rows[i].Cells[0].Value.ToString();
        erow[1] = dataGridView1.Rows[i].Cells[1].Value.ToString();
        erow[2] = dataGridView1.Rows[i].Cells[2].Value.ToString();
        dtr.Rows.Add(erow);
    }

    dataGridView2.DataSource = dtr;
 }
Run Code Online (Sandbox Code Playgroud)

我还在NullReferenceException11 号线收到。

c# ado.net exception-handling datagridview nullreferenceexception

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

获取LinearLayout由Id使用Xamarin返回null

我为什么要这样mainLayout==null

protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);
    SetContentView (Resource.Layout.Main);
    LinearLayout mainLayout = FindViewById<LinearLayout> (Resource.Layout.Main);//return null
    mainLayout.Touch+=(s,e)=>
    {
         //something
    }
}
Run Code Online (Sandbox Code Playgroud)

如何获取MainLayout或如何添加事件处理程序Touch?

Resource.designer.cs:

public partial class Layout
{
    public const int Main = 2130903041;
    //other
}
Run Code Online (Sandbox Code Playgroud)

android nullreferenceexception xamarin.android android-linearlayout xamarin

-1
推荐指数
2
解决办法
6125
查看次数

如何实现接口属性?

以下是我的界面:

public interface IBaseService
{
    List<ExceptionPairs> Exceptions { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

另一个接口是继承它:

public interface IClassStudentsService: IBaseService
{

}
Run Code Online (Sandbox Code Playgroud)

我在下面的类中实现了这个接口:

public class CSService : IClassStudentsService
{
    public List<ExceptionPairs> Exceptions
    {
        get;set;
    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个CSService对象,并尝试访问List 'Exceptions'但收到错误" Object reference not set to an instance of an object."

你能指导我需要做什么实例化吗?

.net c# nullreferenceexception

-1
推荐指数
1
解决办法
74
查看次数

在C#应用程序中获取NullReferenceException并且无法查看解除引用为空的原因

我编写了简单的代码,由于获得NullReferenceException的原因我不知道,由于某些特殊原因无法正常工作.

这是简单的应用程序

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {

        private string[] IPToCheck;
        private List<string> IPRange;
        private bool CorrectNetwork = false;

        public MainPage()
        {
            this.InitializeComponent();
            var hostnames = NetworkInformation.GetHostNames();
            foreach (var hn in hostnames)
            {
                if (hn.IPInformation != null &&
                   (hn.IPInformation.NetworkAdapter.IanaInterfaceType == 71
                   || hn.IPInformation.NetworkAdapter.IanaInterfaceType == 6))
                {
                    IPToCheck = hn.DisplayName.Split(new char[] { '.' });
                    if …
Run Code Online (Sandbox Code Playgroud)

c# nullreferenceexception windows-applications

-1
推荐指数
1
解决办法
59
查看次数

Class数组引用的NullReferenceException

我的代码如下:

int main()
{ 
    CProfile **profiles;
    *profiles  = new CProfile[8];
    profiles[0] = new CProfile(2000,2,4);
    profiles[1] = new CProfile(55000,6,50);
    profiles[2] = new CProfile(758200,5,23);
}
Run Code Online (Sandbox Code Playgroud)

CProfile定义为:

#ifndef PROFILE_H
#define PROFILE_H

class CProfile
{
private: 
    int m_Experience;
    int m_TownhallLevel;
    int m_Trophies;
public:
    CProfile(void);
    CProfile(int,int,int);
    void PrintInfo(void);
};
#endif 
Run Code Online (Sandbox Code Playgroud)

一切似乎都在编译好,但在此过程中会发生NullReferenceException *profiles = new CProfile[8];.我是C++的新手,我似乎无法弄清楚如何正确地实例化一个类.任何帮助将不胜感激,谢谢.

c++ class instance nullreferenceexception

-1
推荐指数
1
解决办法
69
查看次数

SelectMany 中的 Null 条件运算符 - 仍然得到 NullReferenceException

接受的答案@rob

我需要一个.Where()条款/运算符

奖励指向@juharr 直接回答误导性问题

List<Guid> ExistingChildrenIDsFromParentsIcollectionJoinParentChildM2M =
        this.parent
        .SelectMany(
            p => p.joinParentChildM2M?
            .Select(jpc => jpc.ChildID)
            ?? new List<Guid> {Guid.Empty} //tried similar, but didn't understand the type needs of .SelectMany
            )
        .ToList();
Run Code Online (Sandbox Code Playgroud)

原问题:

我在洞里大约有 10 个搜索/40 个结果,但在这件事上一无所获……TIA!

该语句抛出 NullReferenceException :
(p.joinParentChildM2M 有时初始化为 null 有时不初始化)

List<Guid> ExistingChildrenIDsFromParentsIcollectionJoinParentChildM2M =
        this.parent
        .SelectMany(
            p => p.joinParentChildM2M? //shouldn't this Null Conditional Operator break the chain?
            .Select(jpc => jpc.ChildID)
            )
        .ToList()
        ??
        GuidEmptyList();
Run Code Online (Sandbox Code Playgroud)

我试过:
- 移动 Coalesce ?? 在 SelectMany Parens() 中
- 添加 DefaultIfEmpty(意识到空与 …

c# linq nullreferenceexception c#-6.0

-1
推荐指数
1
解决办法
2216
查看次数

表达式主体函数的奇怪行为

当我使用时

    var frontPage = await GetFrontPage();

    protected override async Task<WordDocument> GetFrontPage()
    {
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

这段代码工作正常,我在 frontpage 变量中得到空值。但是当我将函数重写为

protected override Task<WordDocument> GetFrontPage() => null;

Run Code Online (Sandbox Code Playgroud)

我收到了一个NullReferenceException.

谁能帮助我理解这两种说法之间的区别?

.net c# nullreferenceexception async-await expression-body

-1
推荐指数
1
解决办法
77
查看次数