473,320 Members | 1,828 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,320 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 21 '05 #1
10 2570
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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '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 21 '05 #11

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

Similar topics

2
by: Wayne Wengert | last post by:
I am trying to add one column to an existing array (code below). The ReDim command gives the error: ----------------------------------------------- Microsoft VBScript runtime error '800a0009' ...
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...
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: ...
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 ...
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.