集合类操作:未将对象引用设置到对象的实例

以List为例,具体错误信息如下:

未处理 System.NullReferenceException
  Message=未将对象引用设置到对象的实例。
  Source=TestSet
  StackTrace:
       在 TestSet.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\WorkSpace\VS2010\TestSet\TestSet\Form1.cs:行号 34
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 TestSet.Program.Main() 位置 E:\WorkSpace\VS2010\TestSet\TestSet\Program.cs:行号 18
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

先声明一个类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestSet
{
     public class Test
    {
        public Test()
        {
        //
        }
        private List<TestChild> _TestChilds;
        /// <summary>
        /// 子类
        /// </summary>
        public List<TestChild> TestChilds
        {
            set { _TestChilds = value; }
            get { return _TestChilds; }
        }
    }
}

子类如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestSet
{
    public class TestChild
    {
        public TestChild()
        { }
        private string _TestChildsId;
        public string TestChildsId
        {
            set;
            get;
        }
        private string _TestChildsName;
        public string TestChildsName
        {
            set;
            get;
        }
        private string _TestChildsCount;
        public string TestChildsCount
        {
            set;
            get;
        }
    }
}

在测试Form窗体添加如下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestSet
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Test ts = new Test();
            for (int i = 0; i < 10; i++)
            {
                TestChild tsc = new TestChild();
                tsc.TestChildsCount = "10";
                tsc.TestChildsId = Guid.NewGuid().ToString();
                tsc.TestChildsName = "name";

                ts.TestChilds.Add(tsc);
            }
            int ss = ts.TestChilds.Count;
        }
    }
}

小注:这个错误在操作集合类Collection、List时,均会出现,原因是:Test类的属性没有实例化,在添加数据之前给它实例化一下,具体代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestSet
{
    public class Test
    {
        public Test()
        {
            //
        }
        private List<TestChild> _TestChilds;
        /// <summary>
        /// 子类
        /// </summary>
        public List<TestChild> TestChilds
        {
            //以下代码为修正代码
            get
            {
                if (_TestChilds == null)
                {
                    _TestChilds = new List<TestChild>();
                }
                return _TestChilds;
            }
            set
            {
                _TestChilds = value;
            }

        }
    }
}

 

 

时间: 2024-08-03 08:27:32

集合类操作:未将对象引用设置到对象的实例的相关文章

链接数据库时,对文件操作造成脚本异常:未将对象引用设置到对象的实例,感觉是代码出了问题

问题描述 链接数据库时,对文件操作造成脚本异常:未将对象引用设置到对象的实例,感觉是代码出了问题 第一次对文件进行操作,只是简单的添加照片而已,不知道问题出在那里,当我把操作文件那块代码注释后,其他功能都是正常的,代码如下: string fileExtesion = Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower(); if (FileUpload1.HasFile) { string path = Server.MapPa

RadioButtonList赋值操作时报&amp;amp;quot;未将对象引用设置到对象的实例&amp;amp;quot;的错。

问题描述 我在程序中:RadioButtonList_ipsnsex.Items.FindByValue(ds.Tables["Insurance"].Rows[0]["ipsn_sex"].ToString()).Selected=true;code]报错信息为:"未将对象引用设置到对象的实例"但是[code=C#]RadioButtonList_ipsnsex.Items.FindByValue("男"].ToString

未将对象引用设置到对象的实例。哪位能帮帮忙?已经找了几天还没找到问题

问题描述 "/xinwenfabu"应用程序中的服务器错误.--------------------------------------------------------------------------------未将对象引用设置到对象的实例.说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例.源错误:行77:

“/”应用程序中的服务器错误。 未将对象引用设置到对象的实例。请大侠帮忙一下

问题描述 "/"应用程序中的服务器错误.未将对象引用设置到对象的实例.说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例.源错误:执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息.堆栈跟踪:[NullReferenceException:未将对象引用设置到对象的

急需解决“未将对象引用设置到对象的实例”问题~~~

问题描述 我将光盘上的例子考到我自己的机子上,按照说明上的指导,第一步,在SQLServer企业管理器里附加数据库,第二步,修改配置文件,两处,一个是服务器名,一个是访问密码.我用的是VS.NET2.0+SQLServer2000~在我运行的时候,出现下列问题~~未将对象引用设置到对象的实例.说明:执行当前Web请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息.驱六异常详细信息:System.NullReferenceException:未将对象

计算机-未将对象引用设置到对象的实例解决方法?

问题描述 未将对象引用设置到对象的实例解决方法? 计算机-未将对象引用设置到对象的实例解决方法?-未将对象引用到实例"> 大概是这么一个图,有问题可以提问,我会回答,谁知道怎么弄,谢谢 解决方案 ?????? 很多时候在我们运行程序时,会出现"未将对象引用设置到对象的实例"这样的提示信息,就我个人遇到的情况而言,主要是在代码中重复定义了造成的,只要将其中一个删除就可以了.现将从网上收集的各种解决方法汇总如下: 第一种:重复定义造成未将对象引用设置到对象的实例错误 一般在

Silverlight使用时,从工具箱拖拉控件到设计窗体时,抛出异常,System.NullReferenceException 未将对象引用设置到对象的实例。

问题描述 System.NullReferenceException未将对象引用设置到对象的实例.在Microsoft.Windows.Design.Platform.SilverlightMetadataContext.SilverlightXamlExtensionImplementations.<GetXmlNamespaceCompatibilityMappings>d__8.MoveNext()在MS.Internal.Design.Metadata.ReflectionProjec

求教c#,未将对象引用设置到对象的实例问题

问题描述 usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.OleDb;namespacestusystem{publicpartialclassAddStuForm:Form{S

asp.net运行提示未将对象引用设置到对象的实例错误解决方法_实用技巧

未将对象引用设置到对象的实例 一.网络上的一般说法 1.ViewState对象为Null. 2.DateSet空. 3.sql语句或Datebase的原因导致DataReader空. 4.声明字符串变量时未赋空值就应用变量. 5.未用new初始化对象. 6.Session对象为空. 7.对控件赋文本值时,值不存在. 8.使用Request.QueryString()时,所获取的对象不存在,或在值为空时未赋初始值. 9.使用FindControl时,控件不存在却没有做预处理. 10.重复定义造成未