Solved

I get null when initiating sub-class of class (ASP.NET 2.0 / C#). How should I initiate?

Posted on 2007-11-14
3
189 Views
Last Modified: 2010-04-15
I keep getting a null exception when trying to assign value to classes within my class. Here's what I got:

public class ClassOneTest
{

        public ClassOneTest()
        {
            this.Design.PageTitle =  "Acme Corp";
            this.Design.PageTitle =  "Test Title";
        }

        DesignElement _design;
        public DesignElement Design
        {
            get { return _design; }
        }
}

public class DesignElement
{
      private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
       
        private string _pageTitle;
        public string PageTitle
        {
            get { return _pageTitle; }
            set { _pageTitle = value; }
        }
}    

The reason I do it in this format is because I want to access the values like this ClassOneTest.Design.PageTitle because I plan to put more sub-classes than just "Design".

The problem is that I keep getting this error:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 20:         {
Line 21:             this.Design.PageTitle =  "Acme Corp";
Line 22:             this.Design.PageTitle =  "Test Title";
Line 23:         }

I don't understand how to initiate the sub class. I can't make _design = "" because it says it's not the right type. So how do I do this properly? I don't know how to get it started so I can start assigning values to it.
 

0
Comment
Question by:bemara57
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Chumad earned 250 total points
ID: 20284631
Change your constructor:

public ClassOneTest()
        {
            _design = new DesignElement();

            this.Design.PageTitle =  "Acme Corp";
            this.Design.PageTitle =  "Test Title";
        }
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20284990
I would suggest to initialized _design in the declaration:

public class ClassOneTest
{
        DesignElement _design = new DesignElement;

        public ClassOneTest()
        {
            this.Design.PageTitle =  "Acme Corp";
            this.Design.PageTitle =  "Test Title";
        }

        public DesignElement Design
        {
            get { return _design; }
        }
}
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
ID: 20284998
Sorry, the third line should be:
       DesignElement _design = new DesignElement();  // missed parenthesis

0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question