Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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() { }

    }
}
share|improve this question
2  
Instantiate 'Resource` it before using it, e.g. t.Resource = new Resource(); – Tim Medora 16 hours ago
3  
Almost all cases of NullReferenceException are the same. Please see "What is a NullReferenceException in .NET?" for some hints. – John Saunders 16 hours ago

marked as duplicate by Cody Gray, Guvante, Tim Medora, Gary.S, Chris Latta 15 hours ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 2 down vote accepted

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();
    }
}
share|improve this answer

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();
    }
    ...
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.