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

Problem with ReDim statement

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.