473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Array of Textboxes

Hi,

can anyone tell me what ist wrong with this code ?

TextBox[] txtField = new TextBox[4];
txtField[0].Text = "Hello";

I get the error :
An unhandled exception of type 'System.NullReferenceException'
occurred in PentaEngine.exe

Additional information: Object reference not set to an instance of an
object.
Thank you very much for you help.
Frank
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #1
5 9304
This code initializes the array object, but does not initialize the array elements
TextBox[] txtField = new TextBox[4]
That is, when you tries to set the property value, the object txtField[0] doesn't exist, it's a null reference
txtField[0].Text = "Hello"


You must create the objects before using it. For example
txtField[0] = new TextBox()

Nov 15 '05 #2
Hello Frank,

You haven't actually created any TextBox objects, you've only created a
4-element TextBox array with each element initialised to null. You need to
assign a valid TextBox reference to the array elements, e.g.
txtField[0] = new TextBox();
etc.

I hope that helps.
"garak" <fp*******@gmx-dot-de.no-spam.invalid> wrote in message
news:40********@Usenet.com...
Hi,

can anyone tell me what ist wrong with this code ?

TextBox[] txtField = new TextBox[4];
txtField[0].Text = "Hello";

I get the error :
An unhandled exception of type 'System.NullReferenceException'
occurred in PentaEngine.exe

Additional information: Object reference not set to an instance of an
object.
Thank you very much for you help.
Frank
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 15 '05 #3
garak <fp*******@gmx-dot-de.no-spam.invalid> wrote:
can anyone tell me what ist wrong with this code ?

TextBox[] txtField = new TextBox[4];
That creates an array which can hold 4 TextBox references. It doesn't
create any actual TextBoxes. Each element in the array is null.
txtField[0].Text = "Hello";


That then dereferences the null to try to set the Text property - and
fails.

You need to do something like:

TextBox[] txtField = new TextBox[4];
for (int i=0; i < txtField.Length; i++)
{
txtField[i] = new TextBox();
}
txtField[0].Text = "Hello";

(Note that this is not limited to TextBoxes - it's common to all arrays
of reference types.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Yeah thats right...
Thank you very much - you saved my day
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #5
Yeah thats right...
Thank you very much - you saved my day
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bhavin | last post by:
Anybody quickly replies it, would be a great help! I try to create dynamic array of textboxes control in ASP.NET(C#). I got the following error while I tried to assign value to ID property of...
2
by: RBohannon | last post by:
Is it possible to create a control array on an unbound form? I would like to be able to loop through a series of unbound text boxes. Thanks.
1
by: Stephen | last post by:
Was wondering if someone could please help me with an array I'd like to create in an asp.net page. I have to design an array which stores the values of addresses manually entered into textboxes...
3
by: samneefs | last post by:
I'm wondering if there's a way to create an array of textboxes, or loop through textboxes. The textboxes are in an ASP.NET page, but the looping happens through Visual Basic. On page load,...
10
by: Altman | last post by:
In vb 6 you use to be able to create an array of objects and I can't seem to figure this out in .NET. I figured you can't make this but I was wondering how I could do it if I had for instance 3...
3
by: barbara | last post by:
Hi all, I need to use hundreds of textboxes to display data. In VB 6.0, I can assign the value by the loop with a textboxes array. How to do it in VB.net? Thanks a lot.
6
by: Rich | last post by:
Hello, I have an application that contains several checkboxes. I originally created this app in VB.Net 2003 and upgraded the app to VB.Net 2005. I understand the vb2005 supports control...
9
by: jan82 | last post by:
Hi everybody I declared a private static array of textboxes at the top of a class, then i generate the array elements (new textbox()) in a separate function (private void in the same class), ...
1
by: bethmiddy | last post by:
Hello, I am very new to programming and am currently taking a class on VB. This is probably very simple, I just don't get it. I am working on a group project where we need to have 3 columns and...
7
by: Angel López | last post by:
How to do this with Vb.net 2008??? myNumber=1+ Int(Rnd()*90) text(myNumber).backcolor=vbRed Tanks Angel
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.