Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
193 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 1000 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 1000 total points
ID: 20284998
Sorry, the third line should be:
       DesignElement _design = new DesignElement();  // missed parenthesis

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

604 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