473,386 Members | 1,630 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,386 software developers and data experts.

Problem with ReDim statement

I have an Array that I will be resizing with the ReDim statement. For some
reason that I cannot figure out, I am recieving the following error:

[NullReferenceException: Object reference not set to an instance of an
object.]

The code that this occurs in is as follows:

Dim NavButton As NavButtonInfo

Dim TempButtons() As NavButtonInfo

For Each NavButton In ButtonList

If NavButton.parentname = tempparentname Then

ReDim Preserve TempButtons(TempButtons.GetUpperBound(0) + 1)

TempButtons(TempButtons.GetUpperBound(0)) = NavButton

End If

Next
The error occurs on the ReDim line of this code. I am assuming that this has
something to do with the fact that I initially declare TempButtons without
giving it's size, but if I do not have the array the exact size (which is
unknown until runtime) it will cause problems later in my code. For example,
if I place constants like 10 and 5 in the Redim and following lines, it will
get past this section of my code and then give me an error later in my code
when it tries to read from indexes that have not been assigned values. Any
help would be appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
10 1358
Nathan,

The problem with Redims problems is that there are not many people who use
the Redim in dotNet.

It real a method from VB6 and older. By instance the ArrayList or any other
dynamic array give real much more performance, that it is worth to take that
one of those and to recode a little bit.

Here the arraylist, however there are much more collections/arrays/lists
http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor
Nov 19 '05 #2
Hi Nathan

Replace the line
Dim TempButtons() As NavButtonInfo
with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.

HTH

Charles
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...I have an Array that I will be resizing with the ReDim statement. For some
reason that I cannot figure out, I am recieving the following error:

[NullReferenceException: Object reference not set to an instance of an
object.]

The code that this occurs in is as follows:

Dim NavButton As NavButtonInfo

Dim TempButtons() As NavButtonInfo

For Each NavButton In ButtonList

If NavButton.parentname = tempparentname Then

ReDim Preserve TempButtons(TempButtons.GetUpperBound(0) + 1)

TempButtons(TempButtons.GetUpperBound(0)) = NavButton

End If

Next
The error occurs on the ReDim line of this code. I am assuming that this
has something to do with the fact that I initially declare TempButtons
without giving it's size, but if I do not have the array the exact size
(which is unknown until runtime) it will cause problems later in my code.
For example, if I place constants like 10 and 5 in the Redim and following
lines, it will get past this section of my code and then give me an error
later in my code when it tries to read from indexes that have not been
assigned values. Any help would be appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 19 '05 #3
You could also try

Dim TempButtons() As New NavButtonInfo

That seemed to work for me.

-Jerry

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Nathan

Replace the line
Dim TempButtons() As NavButtonInfo


with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.

HTH

Charles
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
I have an Array that I will be resizing with the ReDim statement. For some
reason that I cannot figure out, I am recieving the following error:

[NullReferenceException: Object reference not set to an instance of an
object.]

The code that this occurs in is as follows:

Dim NavButton As NavButtonInfo

Dim TempButtons() As NavButtonInfo

For Each NavButton In ButtonList

If NavButton.parentname = tempparentname Then

ReDim Preserve TempButtons(TempButtons.GetUpperBound(0) + 1)

TempButtons(TempButtons.GetUpperBound(0)) = NavButton

End If

Next
The error occurs on the ReDim line of this code. I am assuming that this
has something to do with the fact that I initially declare TempButtons
without giving it's size, but if I do not have the array the exact size
(which is unknown until runtime) it will cause problems later in my code.
For example, if I place constants like 10 and 5 in the Redim and
following lines, it will get past this section of my code and then give
me an error later in my code when it tries to read from indexes that have
not been assigned values. Any help would be appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Nov 19 '05 #4
"Charles Law" <bl***@nowhere.com> schrieb:
Replace the line
Dim TempButtons() As NavButtonInfo


with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.


Both lines are semantically equal.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 19 '05 #5
Hi Herfried

If they are semantically equal, why do they behave differently?

In the first case, the NullReferenceException exception is thrown, in the
second case it is not.

Charles
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
"Charles Law" <bl***@nowhere.com> schrieb:
Replace the line
Dim TempButtons() As NavButtonInfo


with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.


Both lines are semantically equal.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 19 '05 #6
"Charles Law" <bl***@nowhere.com> schrieb:
If they are semantically equal, why do they behave differently?

In the first case, the NullReferenceException exception is thrown, in the
second case it is not.


Sorry, my bad. I mixed the code up with these two lines which are
semantically equivalent:

\\\
Dim b1() As Button = New Button() {}
Dim b2() As Button = New Button(-1) {}
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 19 '05 #7
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb
"Charles Law" <bl***@nowhere.com> schrieb:
Replace the line
Dim TempButtons() As NavButtonInfo


with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.


Both lines are semantically equal.


1st doesn't create an array. 2nd creates an array with no items.

Armin
Nov 19 '05 #8
When I use the line "Dim TempButtons() As New NavButtonInfo" I recieve the
following error when I build:

Arrays cannot be declared with 'New'.

--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Jerry Spence1" <je**********@somewhere.com> wrote in message
news:42*********************@ptn-nntp-reader03.plus.net...
You could also try

Dim TempButtons() As New NavButtonInfo

That seemed to work for me.

-Jerry

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Nathan

Replace the line
Dim TempButtons() As NavButtonInfo


with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.

HTH

Charles
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
I have an Array that I will be resizing with the ReDim statement. For
some reason that I cannot figure out, I am recieving the following error:

[NullReferenceException: Object reference not set to an instance of an
object.]

The code that this occurs in is as follows:

Dim NavButton As NavButtonInfo

Dim TempButtons() As NavButtonInfo

For Each NavButton In ButtonList

If NavButton.parentname = tempparentname Then

ReDim Preserve TempButtons(TempButtons.GetUpperBound(0) + 1)

TempButtons(TempButtons.GetUpperBound(0)) = NavButton

End If

Next
The error occurs on the ReDim line of this code. I am assuming that this
has something to do with the fact that I initially declare TempButtons
without giving it's size, but if I do not have the array the exact size
(which is unknown until runtime) it will cause problems later in my
code. For example, if I place constants like 10 and 5 in the Redim and
following lines, it will get past this section of my code and then give
me an error later in my code when it tries to read from indexes that
have not been assigned values. Any help would be appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/



Nov 19 '05 #9
This gives me the following error:

Followed by:

Server Application Unavailable
The web application you are attempting to access on this web server is currently unavailable. Please hit the "Refresh" button in your web browser to retry your request.

Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server. Please review this log entry to discover what caused this error to occur.

--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message news:%2****************@TK2MSFTNGP10.phx.gbl...
"Charles Law" <bl***@nowhere.com> schrieb:
If they are semantically equal, why do they behave differently?

In the first case, the NullReferenceException exception is thrown, in the
second case it is not.


Sorry, my bad. I mixed the code up with these two lines which are
semantically equivalent:

\\\
Dim b1() As Button = New Button() {}
Dim b2() As Button = New Button(-1) {}
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 19 '05 #10
Nathan

It wasn't apparent from your original post that the problem is in a web
application.
I don't know if there is some other modification I need to make along with
the Dim TempButtons(-1) As
NavButtonInfo, but it is obviously not fixing my problem.
I think it is fixing the problem that you originally reported, but it has
now exposed another problem. I will have to defer to someone better equipped
to help with diagnosing a web server problem, but you could review the log
entry, as the message suggests, as a start.

Good luck.

Charles
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP14.phx.gbl...
I have tried this, and it gives me the following errors:
After clicking OK, I recieve the following error:

The web application you are attempting to access on this web server is
currently unavailable. Please hit the "Refresh" button in your web browser
to retry your request.
Administrator Note: An error message detailing the cause of this specific
request failure can be found in the application event log of the web server.
Please review this log entry to discover what caused this error to occur.

I don't know if there is some other modification I need to make along with
the Dim TempButtons(-1) As NavButtonInfo, but it is obviously not fixing my
problem.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl... Hi Nathan

Replace the line
Dim TempButtons() As NavButtonInfo


with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.

HTH

Charles
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:ea**************@tk2msftngp13.phx.gbl...
I have an Array that I will be resizing with the ReDim statement. For some
reason that I cannot figure out, I am recieving the following error:

[NullReferenceException: Object reference not set to an instance of an
object.]

The code that this occurs in is as follows:

Dim NavButton As NavButtonInfo

Dim TempButtons() As NavButtonInfo

For Each NavButton In ButtonList

If NavButton.parentname = tempparentname Then

ReDim Preserve TempButtons(TempButtons.GetUpperBound(0) + 1)

TempButtons(TempButtons.GetUpperBound(0)) = NavButton

End If

Next
The error occurs on the ReDim line of this code. I am assuming that this
has something to do with the fact that I initially declare TempButtons
without giving it's size, but if I do not have the array the exact size
(which is unknown until runtime) it will cause problems later in my code.
For example, if I place constants like 10 and 5 in the Redim and
following
lines, it will get past this section of my code and then give me an error
later in my code when it tries to read from indexes that have not been
assigned values. Any help would be appreciated. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/



Nov 19 '05 #11

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

Similar topics

2
by: | last post by:
Is it correct to think that after reducing the populated array's size from say, 10 to 5 with redim preserve myArray(i) an attempt to access an element above the fifth does not cause a...
2
by: Andreas Müller | last post by:
Hi all, VB.NET has the "Redim " statement, which allows me to redimension a System.Array to a new size. I was wondering if something like that is build into the C# language, too? Thanks in...
4
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in...
4
by: Daryl Davis | last post by:
I am having trouble with ReDim (see code below) SaleTable2's structure includes an array for SaleDetailTable2 Dim newsale As New hallsales.SaleTable2 Dim detail As New hallsales.SaleDetailTable2...
5
by: Zenobia | last post by:
Hello, I want to keep a list references to database records being accessed. I will do this by storing the record keys in a list. The list must not contain duplicate keys. So I check the...
9
by: John A Grandy | last post by:
In VB6 you could get away with the following code: Dim Index As Integer Dim ItemsCount As Integer Dim StringArray() As String Dim StringValue As String '....
6
by: Adrian | last post by:
Hi In VB 6 I would declare an array in the general part to make it visible to all parts then once I know how elements I had I would redim it with the amount thus Dim testarray() as string ...
10
by: Nathan Sokalski | last post by:
I have an Array that I will be resizing with the ReDim statement. For some reason that I cannot figure out, I am recieving the following error: The code that this occurs in is as follows: ...
43
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.