By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,830 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,830 IT Pros & Developers. It's quick & easy.

Object reference not set with Array of ImageButtons

P: n/a
ImageButton[] ship;
ship = new ImageButton[5];

for (int i=0; i<5; i++)
{
ship[i].ImageUrl = pathofImage;
ship[i].ID = "ShipNo" + i.ToString();
ship[i].Click += new ImageClickEventHandler(this.ImageBtn_Click);
this.Form1.Controls.Add(ship[i]);
}
I have this peace of code on the start page... and is giving me an
error "Object reference not set to an instance of an object"... I
cannot see what I'm doing wrong. the Array seems good

Any Help?
Thanks

Dec 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Varangian" <of****@gmail.com> a écrit dans le message de news:
11**********************@o13g2000cwo.googlegroups. com...

| ImageButton[] ship;
| ship = new ImageButton[5];
|
| for (int i=0; i<5; i++)
| {
| ship[i].ImageUrl = pathofImage;
| ship[i].ID = "ShipNo" + i.ToString();
| ship[i].Click += new ImageClickEventHandler(this.ImageBtn_Click);
| this.Form1.Controls.Add(ship[i]);
| }
|
|
| I have this peace of code on the start page... and is giving me an
| error "Object reference not set to an instance of an object"... I
| cannot see what I'm doing wrong. the Array seems good

ship = new ImageButton[5];

This code only allocates an array of references to five ImageButtons, it
doesn't create any ImageButtons.

You have to create a new object for each item in the array.

for (int i=0; i<5; i++)
{
ship[i] = new ImageButton();
ship[i].ImageUrl = pathofImage;
ship[i].ID = "ShipNo" + i.ToString();
ship[i].Click += new ImageClickEventHandler(this.ImageBtn_Click);
this.Form1.Controls.Add(ship[i]);
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 18 '05 #2

P: n/a
Hi Varangian,
the problem is that you initialzed the memory space for objects in the
array by saying:

ship = new ImageButton[5];

but that does not create 5 instances of an ImageButton in the array for you
automatically, it just initializes the memory needed so after you create the
array you still need to create the objects you want to put in the aray
seperately. Inside your for loop add a constructor call like:

for (int i=0; i<5; i++)
{
//create the new item
ship[i] = new ImageButton();

...do rest of stuff
}

Hope that helps
Mark Dawson
http://www.markdawson.org
"Varangian" wrote:
ImageButton[] ship;
ship = new ImageButton[5];

for (int i=0; i<5; i++)
{
ship[i].ImageUrl = pathofImage;
ship[i].ID = "ShipNo" + i.ToString();
ship[i].Click += new ImageClickEventHandler(this.ImageBtn_Click);
this.Form1.Controls.Add(ship[i]);
}
I have this peace of code on the start page... and is giving me an
error "Object reference not set to an instance of an object"... I
cannot see what I'm doing wrong. the Array seems good

Any Help?
Thanks

Dec 18 '05 #3

P: n/a
"Varangian" <of****@gmail.com> wrote in
news:11**********************@o13g2000cwo.googlegr oups.com:
ImageButton[] ship;
ship = new ImageButton[5];

for (int i=0; i<5; i++)
{
ship[i].ImageUrl = pathofImage;
ship[i].ID = "ShipNo" + i.ToString();
ship[i].Click += new
ImageClickEventHandler(this.ImageBtn_Click);
this.Form1.Controls.Add(ship[i]);
}
I have this peace of code on the start page... and is giving me
an error "Object reference not set to an instance of an
object"... I cannot see what I'm doing wrong. the Array seems
good


Array elements are zero-based.

The ship array has five elements (0 thru 4), but your "for" loop
iterates over six elements (0 thru 5). The last element (5) does not
exist, so the error occurs.

To prevent problems like this, use the array's length as the limit
for the "for" loop:

for (int i = 0; i < ship.Length; i++)

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Dec 18 '05 #4

P: n/a
Thanks to everyone for the help :) .... always learning. I thought that
I was just filling the array at the same time creating 5 imagebutton
objects.

Dec 18 '05 #5

P: n/a
"Object reference not set to an instance of an object" means that your code
is referencing an object that does not exist. You have 5 lines of code, and
the loop initializer is fine. Therefore, all you have to do is look through
4 lines of code to identify object references in the code that might refer
to objects that are null, or do not exist. This is about as easy as
debugging gets.

Since you are apparently new to debugging, I will walk you through it:
ship[i].ImageUrl = pathofImage;
Object references:
ship (Array)
ship[i] (Array member)
ImageUrl (Property of Array Member)
pathofImage (variable)
ship[i].ID = "ShipNo" + i.ToString();
ship (Array)
ship[i] (Array member)
ID (Property of Array Member)
ship[i].Click += new ImageClickEventHandler(this.ImageBtn_Click);
ship (Array)
ship[i] (Array member)
Click (Event of Array Member)
ImageBtn_Click (Event Handler delegate)
this.Form1.Controls.Add(ship[i]);
Form1 (Form)
Controls (Controls Collection)
ship (Array)
ship[i] (Array member)

In the case of "ship" - Make sure there is an array called "ship".
In the case of "ship[i]" - Make sure the array has 6 members (0 - 5)
In the case of "ImageUrl," "ID," and "Click," - make sure that whatever type
of object the Array holds has these members.
In the case of "Form1" - Make sure there is a form, and that it is named
"Form1".
In the case of "Controls" - Make sure that whatever object "Form1" refers to
has a Controls Collection.
In the case of "ImageBtn_Click" - Make sure that an Event Handler delegate
named "ImageBtn_Click" has been defined in the class.

Looks like an ASP.Net app. Assuming you don't have Visual Studio.Net, or
some other software that has debugging capabilities, you can always use
Trace, or use Response.Write to write data out to the Page, where you can
see it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"Varangian" <of****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... ImageButton[] ship;
ship = new ImageButton[5];

for (int i=0; i<5; i++)
{
ship[i].ImageUrl = pathofImage;
ship[i].ID = "ShipNo" + i.ToString();
ship[i].Click += new ImageClickEventHandler(this.ImageBtn_Click);
this.Form1.Controls.Add(ship[i]);
}
I have this peace of code on the start page... and is giving me an
error "Object reference not set to an instance of an object"... I
cannot see what I'm doing wrong. the Array seems good

Any Help?
Thanks

Dec 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.