简体   繁体   中英

Object reference not set to an instance of an object- “Object includes a list property”

I just create an object of a class. One of the properties of this class is a List of objcet of another class. when I want to cast an object from the second class in this list this error "Object reference not set to an instance of an object" will be given.

This is the code for the first class:

public class RCSection<Bar>
    {
       private string RCSectionName;
       private int NumberOfBars;
       private double NumberOfInnerBars;
       private double NumberOfOuterBars;
       private double TransverseSpacing;


       private Steel LongitudinalSteel;
       private Steel TransevrseSteel;
       private Concrete Concrete;
       private List<Bar> LongitudinalBar;


       private Bar TransverseBar;


       private Section Section;





       public string rCSectionName
       {
           set { RCSectionName = value; }
           get { return RCSectionName; }
       }
       public int numberOfBars
       {
           set { NumberOfBars = value; }
           get { return NumberOfBars; }
       }
       public double transverseSpacing
       {
           set { TransverseSpacing = value; }
           get { return TransverseSpacing; }
       }
       public double numberOfInnerBars
       {
           set { NumberOfInnerBars = value; }
           get { return NumberOfInnerBars; }
       }
       public double numberOfOuterBars
       {
        set { NumberOfOuterBars = value; }
        get { return NumberOfOuterBars; }
       }



       public Steel longitudinalSteel
       {
           set { LongitudinalSteel = value; }
           get { return LongitudinalSteel; }
       }
       public Steel transverseSteel
       {
           set { TransevrseSteel = value; }
           get { return TransevrseSteel; }
       }
       public Concrete concrete
       {
           set { Concrete = value; }
           get { return Concrete; }
       }
       public List<Bar> longitudinalBar
       {
           set { LongitudinalBar = value; }
           get { return LongitudinalBar; }
       }
       public Bar transverseBar
       {
           set { TransverseBar = value; }
           get { return TransverseBar; }
       }



       public Section section
       {
           set { Section = value; }
           get { return Section; }
       }

}

At first I wanna know, the way that I create a property for a list, is it right?! after that, the following code is related to use of object of this class and casting an object in it

  for (int i = 0; i < myRCSection.numberOfBars; i++)
  {

       Bar mybar = new Bar(newFormRCSection.comboBoxSteelSize1.Text,"SI");
       myRCSection.longitudinalBar[i] = mybar;//Error will appear here :(

  }

I found the answer. when I defined the new object of RCSection I should define the List related to this object

RCSection myRCSection= new RCSection<Bar>();
myRCSection.longitudinalBar = new List<Bar>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM