0

I have an application that has multiple dynamic custom user controls to gather edge names and curve offsets in datagridviews. My goal is to enter them into a class with some methods to retrieve some common groups of data to process later.

I have one class that defines the format for the offsets (as it will be the same for both single edges, and a list of edges), and then another class that groups those edges into one "Example". The methods in the second class will return common arrays I need for each Example.

The code is below- complies ok- then when I try to set the sName property of the offSet it returns a NullReferenceException. I suspect I am not initializing things correctly inside of the Example class, or not exposing or declaring them correctly (static, abstract, virtual??). The offSets class works fine, but not when it's accessed within the Example class.

Please help!

    private class offSets
    {
        public string sName { get; set; }
        public double d0 { get; set; }
        public double d1 { get; set; }
        public double d2 { get; set; }
        public double d3 { get; set; }
        public double d4 { get; set; }
        public double d5 { get; set; }
        public double d6 { get; set; }
        public double d7 { get; set; }
        public double d8 { get; set; }
        public double dLength { get; set; }
    }

    private class Example
    {
        public offSets curve1 { get; set; }
        public offSets curve2 { get; set; }
        public List<offSets> lstCurves { get; set; }

        public string testString { get; set; }

        public double[] somemethod()
        {
           //code that returns an array - something lie:
           return this.lstCurves.Select(i => i.d2).ToArray();
        }
    }

    private void temp()
    {
        Example e = new Example();

        e.testString = "This is some test text";

        MessageBox.Show(e.testString);
        // This works as expected.


        e.curve1.sName = "Some Name"; 
        // error on above line: "System.NullReferenceException"
     }
Tyress
  • 3,573
  • 2
  • 22
  • 45
Andy Stagg
  • 373
  • 5
  • 22

4 Answers4

2

The curve1 property is declared, but "empty" (ie. null).

Add a constructor to the Example class, in which you then create the offSets objects:

private class Example
{
    public offSets curve1 { get; set; }
    public offSets curve2 { get; set; }
    public List<offSets> lstCurves { get; set; }

    public string testString { get; set; }

    public Example()
    {
        this.curve1 = new offSets();
        this.curve2 = new offSets();
        this.lstCurves = new List<offSets>();
    }

    public double[] somemethod()
    {
       //code that returns an array - something lie:
       return this.lstCurves.Select(i => i.d2).ToArray();
    }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

This might do the trick for you

Example e = new Example();
e.curve1 = new offSets();
e.curve1.sName = "Some Name";
Mohit S
  • 13,723
  • 6
  • 34
  • 69
0

I wonder if you have to say e.curve1 = new offsets();

before the last line?

Ashraf Iqbal
  • 412
  • 3
  • 11
0

You get an error because you did not initialize curve1. I other words, you get an exception because e.curve1 is null, so e.curve1.sName cannot be assigned.

You need to modify the temp() function:

private void temp()
{
    Example e = new Example();

    e.testString = "This is some test text";

    MessageBox.Show(e.testString);
    // This works as expected.


    e.curve1 = new offSets();
    e.curve1.sName = "Some Name";
    // now it should work
 }
chrumck
  • 3
  • 3
  • maybe this would have worked, but the below answer utilizing a constructor method is for sure a lot more user friendly / removes the possibly that I could forget to initialize the individual classes. Thanks for the answer though – Andy Stagg Feb 15 '16 at 02:31
  • Sure, initializing properties in constructor is the most convenient approach. I would only note that initialized object takes up memory. If you have a large quantity of objects and some properties are not necessarily needed in all instances, you may choose not to initialize them to save space. – chrumck Feb 15 '16 at 03:21