473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object reference not set with Array of ImageButtons

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 ImageClickEvent Handler(this.Im ageBtn_Click);
this.Form1.Cont rols.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
5 6465
"Varangian" <of****@gmail.c om> a écrit dans le message de news:
11************* *********@o13g2 00...legr 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 ImageClickEvent Handler(this.Im ageBtn_Click);
| this.Form1.Cont rols.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 ImageClickEvent Handler(this.Im ageBtn_Click);
this.Form1.Cont rols.Add(ship[i]);
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 18 '05 #2
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 ImageClickEvent Handler(this.Im ageBtn_Click);
this.Form1.Cont rols.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
"Varangian" <of****@gmail.c om> wrote in
news:11******** **************@ o13g2000cwo.goo glegroups.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
ImageClickEvent Handler(this.Im ageBtn_Click);
this.Form1.Cont rols.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
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
"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 ImageClickEvent Handler(this.Im ageBtn_Click);
ship (Array)
ship[i] (Array member)
Click (Event of Array Member)
ImageBtn_Click (Event Handler delegate)
this.Form1.Cont rols.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.c om> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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 ImageClickEvent Handler(this.Im ageBtn_Click);
this.Form1.Cont rols.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2462
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what happens if one of those objects, when it is created, takes a reference to the object that contains it? Do bad things happen? class McShow {
28
20341
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
16
25419
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
3
1348
by: David P. Donahue | last post by:
This is a 2-part question: 1) I have a web form with multiple ImageButtons on it. I'd like them all to do the same thing. Basically, in english-code, the function would be as follows: Set Session variable X to the ImageURL of the button that invoked me; Forward the user to a specific URL; That code is the easy part, of course. I just need to know how to tell the ImageButtons to call that function when they're clicked. Preferably
2
1938
by: flyAway | last post by:
HI In my first asp.Net Homepage I have the following problem: There are some ImageButtons, witch ImageURL constantly changes. Now I would like to create an array, so that I can assign the ImageURLs in a loop. HTML: <TR> <TD><asp:imagebutton id="ImgBtn_1_1" runat="server"
1
1084
by: Jim McGivney | last post by:
On an aspx page with C# code behind I am trying to programmatically change the ImageUrl of various ImageButtons in response to the text contained in corresponding label controls. The following code works:
5
2515
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
1
6813
by: BiraRai | last post by:
is it possible to add new variable to a array object at run time? the following code does not work. $iterator->current()-> = 1234; how can this be done? function bandingCalculator($properties){ $iterator = $properties->getIterator();
275
12366
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
9538
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.