0

I have some when I create my application when I try to use the resourceID From Topic I get this error NullReferenceException my Question is how to use Resource as datatype in Topic object without NullReferenceException

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
    Instantiate 'Resource` it before using it, e.g. `t.Resource = new Resource();` – Tim Medora May 23 '13 at 00:44
  • 3
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 23 '13 at 00:49

2 Answers2

1

Resource is a reference to an object of type Resource. Since you did not specify a constructor, by default it will be set to null since it is a class.

t.Resource.ResourceID attempts to set the ResourceID of the Resource object, which since you have not created, will be null. This creates the NullReferenceException that you are seeing.

You need to initialize t.Resource before you access it. There are two ways to do this:

  1. Add a constructor to Topic that calls the default constructor of Resource
  2. Initialize Resource in Main

In either case you need to add the following line: Resource = new Resource(); (potentially prefixed with t.)

Since the former appears to line up with your expectations, here is Topic with the constructor added.

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

    public Topic()
    {
        Resource = new Resource();
    }
}
Guvante
  • 17,681
  • 1
  • 28
  • 64
0

You need to create a Resource object first:

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

Or do it in the constructor of the Topic:

class Topic
{
    public Topic()
    {
        this.Resource = new Resource();
    }
    ...
}
Daniel Flippance
  • 6,673
  • 3
  • 39
  • 53