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

Can't assign values to array

P: n/a
I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class

I tried to assign some values to the array Test() and display them like this:
Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next
I get an "Object reference not set to an instance of an object." exception on
the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class

I tried to assign some values to the array Test() and display them like this:

Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next
I get an "Object reference not set to an instance of an object." exception on the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy

Jul 21 '05 #2

P: n/a
That's a little strange because the example that I posted was based on an example class that was autogenerated from the
SOAP Proxy Class Wizard in WebMatrix. Here is the class:
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/lead/")> _
Public Class LeadWS_LeadInsert_Input

'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/xml/lead"), _
System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondemand/xml/lead", IsNullable:=false)> _
Public ListOfLead() As Lead1
End Class

So, how would you add a lead to the ListOfLead() array?

--Buddy
Tor Bådshaug wrote:
When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class

I tried to assign some values to the array Test() and display them like


this:

Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next
I get an "Object reference not set to an instance of an object." exception


on
the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy


Jul 21 '05 #3

P: n/a
There are definitely other options besides the one i mentioned.
Just make sure that in one way or the other, that the variable references an
instantiated array object before it is used.
Public ListOfLead() simply declares it. Using "ListOfLead(4)" instantiates
the array object (an array with 5 elements - each element is Nothing, but
the array itself is no longer Nothing).

So, another approach to your code may be to ensure that the array is
initialized from outside the class

clsTest.Test = New String() {"First Item", "Second Item", "Third Item",
"Fourth Item", "Fifth Item"}

Analogously I would use code to add a lead to the ListOfLead-array (assuming
parameter-less constructor of the classes involved).

Dim leadInput As New LeadWS_LeadInsert_Input
leadInput.ListOfLead = New Lead1() { new Lead1 }

If you want to add a new Lead1 object to the array at the end of a current
array (if an initialized and non-empty array already exists), you will have
to add some logic to handle that. (However, there are other datastructures
that
are more appropriate for this.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:#1**************@TK2MSFTNGP15.phx.gbl...
That's a little strange because the example that I posted was based on an example class that was autogenerated from the SOAP Proxy Class Wizard in WebMatrix. Here is the class:
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/
lead/")> _ Public Class LeadWS_LeadInsert_Input

'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _ System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=false)> _ Public ListOfLead() As Lead1
End Class

So, how would you add a lead to the ListOfLead() array?

--Buddy
Tor Bådshaug wrote:
When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public member "Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class

I tried to assign some values to the array Test() and display them like


this:

Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next
I get an "Object reference not set to an instance of an object."
exception
on
the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy


Jul 21 '05 #4

P: n/a
Thanks that worked for initializing an array but how do I add to an alrady initialized array? That is going to be what
I need to do most. Why doesn't System.Array have an append or add method (seems like an obvious need to me)?
--Buddy
Tor Bådshaug wrote:
There are definitely other options besides the one i mentioned.
Just make sure that in one way or the other, that the variable references an
instantiated array object before it is used.
Public ListOfLead() simply declares it. Using "ListOfLead(4)" instantiates
the array object (an array with 5 elements - each element is Nothing, but
the array itself is no longer Nothing).

So, another approach to your code may be to ensure that the array is
initialized from outside the class

clsTest.Test = New String() {"First Item", "Second Item", "Third Item",
"Fourth Item", "Fifth Item"}

Analogously I would use code to add a lead to the ListOfLead-array (assuming
parameter-less constructor of the classes involved).

Dim leadInput As New LeadWS_LeadInsert_Input
leadInput.ListOfLead = New Lead1() { new Lead1 }

If you want to add a new Lead1 object to the array at the end of a current
array (if an initialized and non-empty array already exists), you will have
to add some logic to handle that. (However, there are other datastructures
that
are more appropriate for this.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:#1**************@TK2MSFTNGP15.phx.gbl...
That's a little strange because the example that I posted was based on an


example class that was autogenerated from the
SOAP Proxy Class Wizard in WebMatrix. Here is the class:


<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/
lead/")> _
Public Class LeadWS_LeadInsert_Input

'<remarks/>

<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _

System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=false)> _
Public ListOfLead() As Lead1
End Class

So, how would you add a lead to the ListOfLead() array?

--Buddy
Tor Bådshaug wrote:
When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public
member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work just
fine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
I created a simple class:

Public Class MyTestClass
Public Test() As String
End Class

I tried to assign some values to the array Test() and display them like

this:
Dim clsTest As New MyTestClass
Dim r As String

clsTest.Test.SetValue("First Item", 0)
clsTest.Test.SetValue("Second Item", 1)
clsTest.Test.SetValue("Third Item", 2)
clsTest.Test.SetValue("Fourth Item", 3)
clsTest.Test.SetValue("Fifth Item", 4)

For Each r in clsTest.Test
response.write(r & "<br>")
Next
I get an "Object reference not set to an instance of an object."
exception
on
the line 'clsTest.Test.SetValue("First Item", 0)'.

I don't understand why. WTF.
--Buddy


Jul 21 '05 #5

P: n/a
Hello Buddy,

If you need to add to an array, why not use an ArrayList type instead. It
has the methods you seek.

--- Nick

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:ue**************@TK2MSFTNGP15.phx.gbl...
Thanks that worked for initializing an array but how do I add to an alrady initialized array? That is going to be what I need to do most. Why doesn't System.Array have an append or add method (seems like an obvious need to me)?

--Buddy
Tor Bådshaug wrote:
There are definitely other options besides the one i mentioned.
Just make sure that in one way or the other, that the variable references an instantiated array object before it is used.
Public ListOfLead() simply declares it. Using "ListOfLead(4)" instantiates the array object (an array with 5 elements - each element is Nothing, but the array itself is no longer Nothing).

So, another approach to your code may be to ensure that the array is
initialized from outside the class

clsTest.Test = New String() {"First Item", "Second Item", "Third Item",
"Fourth Item", "Fifth Item"}

Analogously I would use code to add a lead to the ListOfLead-array (assuming parameter-less constructor of the classes involved).

Dim leadInput As New LeadWS_LeadInsert_Input
leadInput.ListOfLead = New Lead1() { new Lead1 }

If you want to add a new Lead1 object to the array at the end of a current array (if an initialized and non-empty array already exists), you will have to add some logic to handle that. (However, there are other datastructures that
are more appropriate for this.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:#1**************@TK2MSFTNGP15.phx.gbl...
That's a little strange because the example that I posted was based on an

example class that was autogenerated from the
SOAP Proxy Class Wizard in WebMatrix. Here is the class:


<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/ lead/")> _
Public Class LeadWS_LeadInsert_Input

'<remarks/>

<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/x ml/lead"), _

System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondeman d/xml/lead", IsNullable:=false)> _
Public ListOfLead() As Lead1
End Class

So, how would you add a lead to the ListOfLead() array?

--Buddy
Tor Bådshaug wrote:

When you get this runtime-error, there is some problem with an object
reference that is not initialized.
In your case, the problem is inside your "MyTestClass". The public


member
"Test()" defines a variable that
refers to an array of strings, but in your code, the array is never
initialized.
If you change your code to the listing below, your code should work

justfine.

Public Class MyTestClass
Public Test(4) As String
End Class

Hope this helps.
Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:Of**************@TK2MSFTNGP11.phx.gbl...
>I created a simple class:
>
> Public Class MyTestClass
> Public Test() As String
> End Class
>
>
>
>I tried to assign some values to the array Test() and display them like
this:
> Dim clsTest As New MyTestClass
> Dim r As String
>
> clsTest.Test.SetValue("First Item", 0)
> clsTest.Test.SetValue("Second Item", 1)
> clsTest.Test.SetValue("Third Item", 2)
> clsTest.Test.SetValue("Fourth Item", 3)
> clsTest.Test.SetValue("Fifth Item", 4)
>
> For Each r in clsTest.Test
> response.write(r & "<br>")
> Next
>
>
>I get an "Object reference not set to an instance of an object."


exception
on
>the line 'clsTest.Test.SetValue("First Item", 0)'.
>
>I don't understand why. WTF.
>
>
>--Buddy


Jul 21 '05 #6

P: n/a
The class wasn't created by me it was auto generated by the WebServices Proxy Class wizard. I'm just trying to figure
out how to use it.
--Buddy
Nick Malik wrote:
Hello Buddy,

If you need to add to an array, why not use an ArrayList type instead. It
has the methods you seek.

--- Nick

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:ue**************@TK2MSFTNGP15.phx.gbl...
Thanks that worked for initializing an array but how do I add to an alrady


initialized array? That is going to be what
I need to do most. Why doesn't System.Array have an append or add method


(seems like an obvious need to me)?

--Buddy
Tor Bådshaug wrote:
There are definitely other options besides the one i mentioned.
Just make sure that in one way or the other, that the variable
references an
instantiated array object before it is used.
Public ListOfLead() simply declares it. Using "ListOfLead(4)"
instantiates
the array object (an array with 5 elements - each element is Nothing,
but
the array itself is no longer Nothing).

So, another approach to your code may be to ensure that the array is
initialized from outside the class

clsTest.Test = New String() {"First Item", "Second Item", "Third Item",
"Fourth Item", "Fifth Item"}

Analogously I would use code to add a lead to the ListOfLead-array
(assuming
parameter-less constructor of the classes involved).

Dim leadInput As New LeadWS_LeadInsert_Input
leadInput.ListOfLead = New Lead1() { new Lead1 }

If you want to add a new Lead1 object to the array at the end of a
current
array (if an initialized and non-empty array already exists), you will
have
to add some logic to handle that. (However, there are other
datastructures
that
are more appropriate for this.

Hope this helps.

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.

"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
news:#1**************@TK2MSFTNGP15.phx.gbl...
That's a little strange because the example that I posted was based on
an
example class that was autogenerated from the
SOAP Proxy Class Wizard in WebMatrix. Here is the class:


<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:crmondemand/ws/
lead/")> _
Public Class LeadWS_LeadInsert_Input

'<remarks/>
<System.Xml.Serialization.XmlArrayAttribute([Namespace]:="urn:/crmondemand/x
ml/lead"), _
System.Xml.Serialization.XmlArrayItemAttribute([Namespace]:="urn:/crmondeman
d/xml/lead", IsNullable:=false)> _
Public ListOfLead() As Lead1
End Class

So, how would you add a lead to the ListOfLead() array?

--Buddy
Tor Bådshaug wrote:
>When you get this runtime-error, there is some problem with an object
>reference that is not initialized.
>In your case, the problem is inside your "MyTestClass". The public

member
>"Test()" defines a variable that
>refers to an array of strings, but in your code, the array is never
>initialized.
>If you change your code to the listing below, your code should work
just
fine.
>
> Public Class MyTestClass
> Public Test(4) As String
> End Class
>
>Hope this helps.
>Tor Bådshaug
>tor.badshaug [//at\\] bekk.no.
>
>"Buddy Ackerman" <bu**********@buddyackerman.com> wrote in message
>news:Of**************@TK2MSFTNGP11.phx.gbl. ..
>
>
>
>>I created a simple class:
>>
>> Public Class MyTestClass
>> Public Test() As String
>> End Class
>>
>>
>>
>>I tried to assign some values to the array Test() and display them
like
this:
>
>
>
>> Dim clsTest As New MyTestClass
>> Dim r As String
>>
>> clsTest.Test.SetValue("First Item", 0)
>> clsTest.Test.SetValue("Second Item", 1)
>> clsTest.Test.SetValue("Third Item", 2)
>> clsTest.Test.SetValue("Fourth Item", 3)
>> clsTest.Test.SetValue("Fifth Item", 4)
>>
>> For Each r in clsTest.Test
>> response.write(r & "<br>")
>> Next
>>
>>
>>I get an "Object reference not set to an instance of an object."

exception
>on
>
>
>
>>the line 'clsTest.Test.SetValue("First Item", 0)'.
>>
>>I don't understand why. WTF.
>>
>>
>>--Buddy
>
>
>


Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.