如何在没有NullReferenceException的情况下使用class作为数据类型?

时间:2013-05-23 00:43:24

标签: c#

我在创建应用程序时有一些当我尝试使用resourceID时从Topic我得到此错误NullReferenceException我的问题是如何在没有NullReferenceException的Topic对象中使用Resource作为数据类型

System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=test
  StackTrace:
       at test.Program.Main(String[] args) in C:\Users\Abdalla\Desktop\BOL\BOL\test\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 


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

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            Topic t = new Topic();

            t.Id = 1;
            t.Drescription = "Topic Drescription ";
            t.Resource.ResourceID = 1;


            Console.WriteLine("ResourceID" + t.Resource.ResourceID.ToString());
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BOL
{
    class Topic
    {
        public int Id { get; set; }
        public int Drescription { get; set; }
        public Resource Resource { get; set; }
    }
}

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

namespace BOL
{
    class Resource
    {
        public int ResourceID { get; set; }
        public string Res_summary { get; set; }
        public string PageID { get; set; }
        public Guid UserID { get; set; }
        public bool Enabled { get; set; }

        public Resource() { }

    }
}

2 个答案:

答案 0 :(得分:1)

Resource是对Resource类型对象的引用。由于您未指定构造函数,因此默认情况下它将设置为null,因为它是class

t.Resource.ResourceID尝试设置ResourceID对象的Resource,因为您尚未创建,所以null将为NullReferenceException。这会创建您正在看到的t.Resource

您需要在访问之前初始化Topic。有两种方法可以做到这一点:

  1. Resource添加一个构造函数,该构造函数调用Resource
  2. 的默认构造函数
  3. Main
  4. 中初始化Resource = new Resource();

    在任何一种情况下,您都需要添加以下行:t.(可能以Topic为前缀)

    由于前者似乎符合您的期望,因此class Topic { public int Id { get; set; } public int Drescription { get; set; } public Resource Resource { get; set; } public Topic() { Resource = new Resource(); } } 添加了构造函数。

    {{1}}

答案 1 :(得分:0)

您需要先创建一个Resource对象:

t.Resource = new Resource();
t.Resource.ResourceID = 1;

或者在主题的构造函数中执行:

class Topic
{
    public Topic()
    {
        this.Resource = new Resource();
    }
    ...
}