Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:00:52+00:00 2026-06-07T02:00:52+00:00

I am attempting to solve an exercise from a book. (with no published answer)

  • 0

I am attempting to solve an exercise from a book. (with no published answer)

I need to reference a PictureBox object which is present on my form to an Object Array. (I need to assign four of them really)

I initialise the array and assign the variables to it. I then call a method within each item in the array however the PictureBox object is not assigned. (Null Exception)

I’m a little baffled as I’ve found snippets of code publicly on the net which show I’m doing this correctly.

Code Below for pointers please:

Main Class

public partial class Form1 : Form
{
    Greyhound[] greyhoundArray = new Greyhound[4];

    public Form1()
    {
        greyhoundArray[0] = new Greyhound() { Location = 0, MyPictureBox = dog1, RaceTrackLenght = 100, StartingPosition = 0 };
        greyhoundArray[1] = new Greyhound() { Location = 0, MyPictureBox = dog2, RaceTrackLenght = 100, StartingPosition = 0 };
        greyhoundArray[2] = new Greyhound() { Location = 0, MyPictureBox = dog3, RaceTrackLenght = 100, StartingPosition = 0 };
        greyhoundArray[3] = new Greyhound() { Location = 0, MyPictureBox = dog4, RaceTrackLenght = 100, StartingPosition = 0 };

        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        foreach (Greyhound greyhound in greyhoundArray)
        {
            greyhound.Run();
        }
    }
}

Greyhound Class

public class Greyhound
{
    public int StartingPosition;
    public int RaceTrackLenght;
    public PictureBox MyPictureBox;
    public int Location = 0;
    public Random Randomiser;

    public void Run()
    {
        // MessageBox.Show(MyPictureBox.Name + " was called");
        Randomiser = new Random();

        int distance = Randomiser.Next(0, 4);

        Point p = MyPictureBox.Location;
        p.X += distance;
        MyPictureBox.Location = p;
    }

    public void TakeStartingPosition()
    { }
}

Also I can confirm each dog PictureBox does exist on the form:

snippet from Form1.Designer.cs

// 
// dog1
// 
this.dog1.Image = ((System.Drawing.Image)(resources.GetObject("dog1.Image")));
this.dog1.Location = new System.Drawing.Point(17, 21);
this.dog1.Name = "dog1";
this.dog1.Size = new System.Drawing.Size(71, 26);
this.dog1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.dog1.TabIndex = 2;
this.dog1.TabStop = false;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:00:54+00:00Added an answer on June 7, 2026 at 2:00 am

    Place the call to InitializeComponent() before the initialization of GreyHound array.

    When you call the initialization of the Greyhound array, you have not called yet the this.dog1 = new PictureBox() inside InitializeComponent, so you copy a null into the MyPictureBox property of each Greyhound instances

    Also, I think you have a problem with the Randomiser variable in your Greyhound class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to solve a set of equations of the form Ax = 0.
Greetings! I am attempting to solve a few performance issues which are cropping up
I am attempting to use tortoiseSVN to solve a small problem which I am
I am attempting to solve a problem from topcoder.com and it's driving me crazy.
I'm attempting to solve a problem in which decltype will greatly simplify things, but
I came across another codechef problem which I am attempting to solve in Scala.
I am attempting to solve Project Euler problem 10 where the user is asked
I'm attempting to solve the Project Euler Problem 8 with C++, and the problem
I have sort of a tricky problem I'm attempting to solve. First of all,
Attempting to build a C# NPAPI plugin I have found a tutorial which describes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.