Problem with ReDim statement

N

Nathan Sokalski

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.
 
C

Cor Ligthert

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/d...frlrfsystemcollectionsarraylistclasstopic.asp

I hope this helps,

Cor
 
C

Charles Law

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 said:
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.
 
J

Jerry Spence1

You could also try

Dim TempButtons() As New NavButtonInfo

That seemed to work for me.

-Jerry

Charles Law said:
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 said:
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.
 
H

Herfried K. Wagner [MVP]

Charles Law said:
Replace the line


with

Dim TempButtons(-1) As NavButtonInfo

This will initialise the array with no elements.

Both lines are semantically equal.
 
C

Charles Law

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
 
H

Herfried K. Wagner [MVP]

Charles Law said:
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) {}
///
 
N

Nathan Sokalski

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
(e-mail address removed)
http://www.nathansokalski.com/

Jerry Spence1 said:
You could also try

Dim TempButtons() As New NavButtonInfo

That seemed to work for me.

-Jerry

Charles Law said:
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 said:
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.
 
N

Nathan Sokalski

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.
 
C

Charles Law

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


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
(e-mail address removed)
http://www.nathansokalski.com/

Charles Law said:
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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

For loop with if statement 3
ReDim Problem 2
Need an if statement 8
Problem with codewars. 5
REDIM Preserve problem 4
Problem with KMKfw libraries 1
Java matrix problem 3
Freeing Dynamic Arrays 1

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top