0

当我尝试使用来自 Topic 的 resourceID 时,我有一些创建应用程序时出现此错误 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() { }

    }
}
4

2 回答 2

1

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

t.Resource.ResourceID尝试设置对象ResourceIDResource,因为您还没有创建,所以它将是null. 这创造了NullReferenceException你所看到的。

您需要t.Resource在访问它之前进行初始化。有两种方法可以做到这一点:

  1. 添加一个构造函数来Topic调用默认构造函数Resource
  2. 初始化Resource_Main

无论哪种情况,您都需要添加以下行:(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();
    }
}
于 2013-05-23T00:52:16.867 回答
0

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

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

或者在 Topic 的构造函数中做:

class Topic
{
    public Topic()
    {
        this.Resource = new Resource();
    }
    ...
}
于 2013-05-23T00:47:00.323 回答