Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code you have has several severe problems:</p> <ol> <li>The reason for your <code>NullReferenceException</code> is two-fold: <ol> <li>You declare an array of <code>GreyHound</code>, but you never initialize it. That means, that <code>dog</code> is <code>null</code> and trying to access <code>dog[1]</code> results in a NullReferenceException.</li> <li>If you would fix that, you would still have another problem:<br> Assuming that <code>GreyHound</code> is a reference type that means that you will have to create an instance for each item in the array. With your current code, all items in the array are <code>null</code> which leads to a <code>NullReferenceException</code> when trying to access the property <code>MyPictureBox</code> on any item in the array, because <code>dog[1]</code> would be <code>null</code>.</li> </ol></li> <li>For every item in the array, you first assign a new instance of a <code>PictureBox</code> to the property <code>MyPictureBox</code> but you overwrite that value in the very next line with <code>pictureBoxX</code>.</li> <li>As we are in C#, arrays are zero indexed, so the first item has the index 0 and not 1.</li> </ol> <p>Fix your code like so:</p> <pre><code>GreyHound[] dog = new GreyHound[4]; // solves problem 1.1 dog[0] = new GreyHound(); // solves problem 1.2 dog[0].MyPictureBox = pictureBox1; dog[1] = new GreyHound(); // solves problem 1.2 dog[1].MyPictureBox = pictureBox2; dog[2] = new GreyHound(); // solves problem 1.2 dog[2].MyPictureBox = pictureBox3; dog[3] = new GreyHound(); // solves problem 1.2 dog[3].MyPictureBox = pictureBox4; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload