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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Obtain cell value using column ID in DataGrid using VB.NET 2 25
hbo knew my windows software 4 62
Finding the IIS version 5 21
reading excel file in .net 2 15
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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