Click here to Skip to main content
15,885,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am getting an error using

VB
Structure JobItem
       Dim Index As Integer
       Dim Url As String
       Dim Status As String
       Dim WorkData As String
       Dim ListUrls As List(Of Uri)
   End Structure


when i try to add to ListUrls like this

VB
Dim jb As New JobItem
jb.ListUrls.Add(urlNext)


i get an error Object reference not set to an instance of an object.

how do i fix this?
Posted
Updated 5-Jun-11 15:15pm
v3

This is what it is: you did not instantiate one of the reference-type objects, so one of them is null, or some code explicitly assigned null to the variable. In your case, this is uninitialized ListUrls. After you constructed the structure, it's null. Use New to create a list. Add a constructor to the structure and initialize everything. You should use a constructor with parameters Index, Url, Status, and WorkData.

Using string types in inappropriate. Status is likely to be enumeration, Url must be of Url type; I don't know what's WorkData and why, but also unlikely the string type.

—SA
 
Share this answer
 
v3
Comments
Cool Smith 5-Jun-11 15:41pm    
can you give sample code, i know that is what i need to do, but how?
Sergey Alexandrovich Kryukov 5-Jun-11 15:43pm    
There is nothing to code here. What exactly do you expect?
--SA
Sergey Alexandrovich Kryukov 5-Jun-11 15:43pm    
See the updated answer.
--SA
Tarun.K.S 5-Jun-11 16:22pm    
Well explained SA. 5+
Sergey Alexandrovich Kryukov 5-Jun-11 16:50pm    
Thank you, Tarun.
--SA
If you want the code, well here it is but do read SAKryukov's excellent explanation on why there is an error.

VB
Dim jb As New JobItem
Dim newList As New List(Of Uri)
newList.Add(New Uri("some path", UriKind.RelativeOrAbsolute))
jb.ListUrls = newList


It should work now.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jun-11 16:52pm    
Right, my 5 (than you for translating all this in VB.NET which I never install). Adding constructor to the structure would be more consistent though.
--SA
Tarun.K.S 6-Jun-11 2:59am    
Thanks. True, that's why I advised him to have a look at your answer. :)
Not really an answer to your question, but something I want to point out. Using a Structure is usually not the best way to go. Classes are the best way to go in probably 99,99% of the time.
Read here when to use Structures[^].
I think by using the List(Of T) you pretty much break those rules.
I am not saying that using a Structure is plain wrong (you probably have your reasons) I just want to point out that maybe you could go for a Class instead.
Also taken the arguments of SAKryukov you probably need to make Status an Enum, Url an Url Class etc. By creating a Class you will have more flexibility and you will be sure everything is ok in the future.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Jun-11 20:54pm    
That makes sense, my 5. Not sure about 99.99% of time (probably an exaggeration; also, it depends in many factors), but a choice between structure and class should be taken with responsibility, as well as many other choices on types.
--SA
Sander Rossel 6-Jun-11 2:08am    
Personally I have never made a Structure, but lots of Classes. Possibly because my boss thinks Structures are some medieval programming technique coming from VB6 and before and we should not use them even if our lives depended upon it :laugh:
Don't know where he got that idea...
Sergey Alexandrovich Kryukov 6-Jun-11 11:32am    
Probably you depend on your boss too much... :-) Structures are certainly good in many cases (and have nothing, absolutely nothing to do with VB!). I do use them, much less then classes though. And they are reasonably used in Microsoft's .NET libraries, including those I consider as very good ones.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900