-
Feb 11th, 2021, 04:04 PM
#1
Thread Starter
New Member
NullReferenceException when adding list
Hello,
I am trying to add something to a list, but I get a System.NullReferenceException: 'Object reference not set to an instance of an object', this is my code:
Code:
Module Module1
Structure tList
Dim sgName As String
Dim sgTest As String
Dim stModule As List(Of tModule)
End Structure
Structure tModule
Public sgName As String
Public sgTest As String
End Structure
Sub Main()
Dim l_stModule As New tModule
Dim l_stList As New tList
l_stModule.sgName = "Module"
l_stModule.sgTest = "Test"
l_stList.stModule.Add(l_stModule)
End Sub
End Module
I don't understand how to solve this.
-
Feb 11th, 2021, 04:10 PM
#2
Re: NullReferenceException when adding list
replace
Code:
Dim stModule As List(Of tModule)
by
Code:
Dim stModule As new List(Of tModule)
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Feb 11th, 2021, 04:15 PM
#3
Re: NullReferenceException when adding list
Welcome to VBForums
Initially a List(of ...) is not set to anything (it is not even an empty list), so you need to initialise it appropriately by assigning an empty list to it.
You can do that either after you create it:
Code:
Dim l_stList As New tList
l_stList.stModule = New List(of tModule)
...or possibly within the declaration itself:
Code:
Dim stModule As New List(Of tModule)
edit: it seems I typed slowly!
-
Feb 11th, 2021, 04:22 PM
#4
Thread Starter
New Member
Re: NullReferenceException when adding list
Hello, thanks to both of you for the warm and fast welcome.
I understand. If initialise the list within the declaration VS show the error BC30795 / Non-shared member in a Structure cannot be declared 'New'. As the list will be used several times I find it easier to use the solution within the declaration - is it safe/okay to use the Shared declaration?
-
Feb 11th, 2021, 04:25 PM
#5
Re: NullReferenceException when adding list
Well it means that something on the line on which occurred us null or nothing. Your best bet is to examine each object from left to right when it happens. From experience, I can tell right off the bat what the problem is... you've got somehting that isn't initialized before you use it. Declaring an object doesn't make it exist magically. You need to create it first. Even if it is inside a structure.
-tg
-
Feb 11th, 2021, 05:29 PM
#6
Re: NullReferenceException when adding list
Originally Posted by IronJosie
Hello, thanks to both of you for the warm and fast welcome.
I understand. If initialise the list within the declaration VS show the error BC30795 / Non-shared member in a Structure cannot be declared 'New'. As the list will be used several times I find it easier to use the solution within the declaration - is it safe/okay to use the Shared declaration?
Ah, yes I forgot that...Shared means the list will be shared by all instances of the structure, if you have only one instance, it can be ok else you may have some suprise. It should be better to replace the structure by a class
Code:
public class tList
Dim sgName As String
Dim sgTest As String
Dim stModule As new List(Of tModule)
End class
public class tModule
Public sgName As String
Public sgTest As String
End class
Last edited by Delaney; Feb 11th, 2021 at 05:44 PM.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Feb 12th, 2021, 05:16 AM
#7
Thread Starter
New Member
Re: NullReferenceException when adding list
Well, the learning curve coming from VB pretty steep, but I like it. The error form my first post was a reduction to point out the issues that I had. I am trying to male my way from there and I have updated my code to this:
Code:
Module Module1
Class clList
Public sgNameList As String
Public sgTestList As String
Public lsModule As New List(Of clModule)
End Class
Class clModule
Public sgName As String
Public sgTest As String
End Class
Sub Main()
Dim l_clList As New clList
'Dim l_cModule As New cModule
For i16Loop As Integer = 1 To 10
Dim l_clModule As New clModule With {
.sgName = "Name " & i16Loop,
.sgTest = "Test " & i16Loop
}
l_clList.lsModule.Add(l_clModule)
Next
End Sub
End Module
If I understand it correctly, I have to create a new instance of l_clModule per loop iteration, otherwise the reference in the list is still to the old instance. Is the implementation quite right, or is there something that can be done better?
-
Feb 12th, 2021, 09:58 PM
#8
Re: NullReferenceException when adding list
Move the classes outside the module. You can leave them in the same file, or put each in its own file, but they shouldn't be inside the module.
In .NET, a Module IS a class...essentially. It's just a class where every member is shared, but you don't have to declare them as shared. This was done to make a module act kind of like a module in VB6. Instead of a module, you could just create a class and make every member shared, but then you'd have to use the class name as well as the member name:
Module1.Main()
rather than just plain:
Main()
Because you used a Module, you don't need to use Module1. Had you used a class with all shared members, then you would need to use the class name. Therefore, modules are somewhat easier than using a class with all shared members....but their the same thing. They only work differently so that VB.NET looks more like VB6.
Still, the bottom line is that a Module is a class, so you have defined two classes within a separate class. There are times when that is the right thing to do, but it usually isn't. In this case, it would be better not to do so. Thus, move the classes out of the module.
My usual boring signature: Nothing
-
Feb 12th, 2021, 10:01 PM
#9
Re: NullReferenceException when adding list
Another point is that structures are MUCH less desirable in .NET. You should only use a structure if there are only a few members (like two or three), and they are all value types (integer, double, etc.). If you have a member that is a reference type (EVERYTYHING else), then a class is the right thing to use instead of a structure.
For this reason, I have rarely used structures. They have some interesting characteristics when you use them in an array such that they don't always behave the way you expect them to if you don't give it much thought. They don't behave the way classes do in the same situation, while classes behave the way you expect them to. So, you usually want to use classes over structures.
My usual boring signature: Nothing
-
Feb 14th, 2021, 08:38 AM
#10
Thread Starter
New Member
Re: NullReferenceException when adding list
Thank you very much for the detailled explanation!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|