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
183 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now