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
184 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

863 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

23 Experts available now in Live!

Get 1:1 Help Now