0

Apologies for what is probably a dumb question, but I think I've boiled my code down the the core issues, of which there are two that are driving me nuts. I'd appreciate any assistance.

In the code below, the first issue is when my debug gets to the line 'bus[0]..' I get the 'Object reference not set to an instance of an object', even though I just instantiated the class in the previous line.

My next issue is that the 'SystemArrays' class is intended to be a repository, visible by all my classes (such as 'Solver'), where they can Get & Set its public properties. However, I can't figure how or where to instantiate the class to make it visible to everyone.

Any help would be greatly appreciated. Thanks.

    public Form1()
    {
        InitializeComponent();
    }

    SystemArrays newArray = new SystemArrays();

    private void button1_Click(object sender, EventArgs e)
    {
        Bus[] bus = new Bus[3];
        bus[0].elementNum = 5;
        bus[1].elementNum = 8;
        bus[2].elementNum = 26;

        newArray.buses[0].elementNum = bus[0].elementNum;
    }

    public class SystemArrays
    {
        public Bus[] buses { get; set; }
    }

    public class Bus
    {
        public int elementNum { get; set; }
    }

    public class Solver
    {
        // int x = newArray.buses[0].elementNum;
    }
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
J. McCabe
  • 115
  • 2
  • 9
  • Unfortunately post contains 2 unrelated questions - thus main is closed as duplicate, answer to the second one can be found using search https://www.bing.com/search?q=c%23%20property%20accessible%20to%20all%20classes and closed as duplicate of http://stackoverflow.com/questions/7679230/how-to-make-a-object-accessible-through-all-files-in-c-sharp for example. – Alexei Levenkov Jan 28 '17 at 16:58

3 Answers3

2

1. You are initializing the array, not initializing the objects inside the array and hence getting the Object reference not set to an instance of an object exception. So, you need to make a code change like

Bus[] bus = new Bus[3];
bus[0].elementNum = 5;

To

Bus[] bus = new Bus[3];
bus[0] = new Bus();
bus[0].elementNum = 5;

2. To make newArray public, set the right accessibility by specifying the correct protection level like public as you need to expose the class to other classes. Also, you can declare it as static if you need a single copy of newArray, though it is better if you create a get property for this kind of scenario.

public static SystemArrays newArray = new SystemArrays();
Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41
  • Wow, guys thanks. That's really embarrassing. I keep making that same mistake over and over and over. Again, my apoligies for the dumb question – J. McCabe Jan 28 '17 at 16:46
  • What is embarrassing that this answer is even posted - question contains 2 unrelated questions and first one is clearly explained in well-known duplicate. – Alexei Levenkov Jan 28 '17 at 16:56
1

You are getting 'Object reference not set to an instance of an object' exception because that is exactly what is happening. You have created the array of 5 elements (busses), but you have not initialized them. After you create an array, all its items contain default value of the type of the array. In your case type of the array is Bus (reference type) and default value of all reference types is null

You can initialize values of your array by simply assigning:

bus[0] = new Bus();
Andrius
  • 2,247
  • 3
  • 14
  • 15
1

The problem is that you're instantiating the array, but not the single instances. You can use the following compact way:

var buses = new Bus[3]  // instantiate an array object of size 3
{ 
   new Bus() { elementNum = 5 }, // instantiate new Bus object
   new Bus() { element = 8 },    // and here 
   new Bus() { elementNum = 26 }  // and here
};
peval27
  • 1,239
  • 2
  • 14
  • 40