473,320 Members | 1,900 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.

I cannot add an item if no item exists before

Hi!

If a collection ( here; a List) is Nothing then I cannot use .Add method,
It causes an error:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

First I try .Add method, then I try to create an instance and use it, if error occurs.

Am I doing it right or is there a better way to resolve this error?

************************************************** **
Public Structure WingStructure
Public ShipType As String
Public ShipCount As Integer
End Structure

Public Structure FleetStructure
Public Wings As List(Of WingStructure)
...
End Structure

...
Dim Fleet As New FleetStructure

'Dummy objects
Dim _wing As New WingStructure
Dim _wings As New List(Of WingStructure)

_wing.ShipCount = 2
_wing.ShipType = "Fighter"

Try
Fleet.Wings.Add(_wing)
Catch ex As Exception
wings.Add(_wing)
Fleet.Wings = _wings
End Try

Jul 26 '06 #1
2 981
Hi,

You need to create the list before you use it.

Dim Fleet As New FleetStructure

Fleet.Wings = new List(Of WingStructure)

Ken
--------------------
"nime" <ea****@planev.comwrote in message
news:O4**************@TK2MSFTNGP06.phx.gbl...
Hi!

If a collection ( here; a List) is Nothing then I cannot use .Add method,
It causes an error:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

First I try .Add method, then I try to create an instance and use it, if
error occurs.

Am I doing it right or is there a better way to resolve this error?

************************************************** **
Public Structure WingStructure
Public ShipType As String
Public ShipCount As Integer
End Structure

Public Structure FleetStructure
Public Wings As List(Of WingStructure)
...
End Structure

...
Dim Fleet As New FleetStructure
'Dummy objects
Dim _wing As New WingStructure
Dim _wings As New List(Of WingStructure)
_wing.ShipCount = 2
_wing.ShipType = "Fighter"
Try
Fleet.Wings.Add(_wing)
Catch ex As Exception
wings.Add(_wing)
Fleet.Wings = _wings
End Try
Jul 27 '06 #2
Thank you very muck.
"Ken Tucker [MVP]" <vb***@bellsouth.netwrote in message news:OT****************@TK2MSFTNGP04.phx.gbl...
Hi,

You need to create the list before you use it.

Dim Fleet As New FleetStructure

Fleet.Wings = new List(Of WingStructure)

Ken
--------------------
"nime" <ea****@planev.comwrote in message news:O4**************@TK2MSFTNGP06.phx.gbl...
Hi!

If a collection ( here; a List) is Nothing then I cannot use .Add method,
It causes an error:
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."

First I try .Add method, then I try to create an instance and use it, if error occurs.

Am I doing it right or is there a better way to resolve this error?

************************************************** **
Public Structure WingStructure
Public ShipType As String
Public ShipCount As Integer
End Structure

Public Structure FleetStructure
Public Wings As List(Of WingStructure)
...
End Structure

..
Dim Fleet As New FleetStructure
'Dummy objects
Dim _wing As New WingStructure
Dim _wings As New List(Of WingStructure)
_wing.ShipCount = 2
_wing.ShipType = "Fighter"
Try
Fleet.Wings.Add(_wing)
Catch ex As Exception
wings.Add(_wing)
Fleet.Wings = _wings
End Try

Jul 27 '06 #3

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

Similar topics

6
by: Chad Crowder | last post by:
Getting the following error on my production server whether the file exists or not: "System.IO.IOException: Cannot create a file when that file already exists." Here's the code generating the...
1
by: Patrick F | last post by:
I keep getting this error message and i don't know whats the problem is and how to solve it. I have a webproject with 2 components (dll) added to it. One is called PhotoGallery and one is called...
0
by: Dhananjay | last post by:
Hi All, I want to develop one application in vb.net for exchange 2000. I tried to add one contact with the code snippet below. The same logic is there for appointment on Microsoft's site. (I...
8
by: charli | last post by:
Error 3265, "Item cannot be found in the collection corresponding to the requested name or ordinal" code programatically opens a query using ADOX and changed the sql Dim cat As New...
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...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.