NullReferenceException

jpp3773

New member
Joined
May 2, 2006
Messages
3
Programming Experience
1-3
I hope this is the right forum. I am getting a System.NullReferenceException in my code. I have googled and haven't had any success. I use to be faily decent at VB, but haven't used it in a couple years so I am a bit rusty.

This is my Module1.vb
VB.NET:
Imports System.IO

Public Structure MonthData
    Public aDayData() As DayData
    Public intMonth As Integer
End Structure

Public Structure DayData
    Public aHourData() As HourData
    Public intDay As Integer
End Structure

Public Structure HourData
    Public aMinuteData() As MinuteData
    Public intHour As Integer
End Structure

Public Structure MinuteData
    Public aCarData() As CarData
    Public intMinute As Integer
End Structure

Public Structure CarData
    Public intCarSpeed As Integer
End Structure

Module Module1
    Public strMonthData As String
    Public sMonth As MonthData
End Module

This is my Form1.vb - I have ommited some code.
VB.NET:
Imports System.IO
Imports RoadSpeedAnalyzer.Module1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sMonth As MonthData
        sMonth.intMonth = 0
        sMonth.aDayData(0).intDay = 3
 sMonth.aDayData(0).aHourData(0).aMinuteData(0).aCarData(0).intCarSpeed = 35
    End Sub

My error occurrs on these lines.
VB.NET:
sMonth.aDayData(0).intDay = 3
       sMonth.aDayData(0).aHourData(0).aMinuteData(0).aCarData(0).intCarSpeed = 35
 
try:
VB.NET:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sMonth As New MonthData
 
thanks... i tried that and removing my duplicate declarations for
Dim sMonth As MonthData that were in both module1 and form1 but netiher seemed to work =[
 
MonthData is a structure so you don't have to invoke a constructor, i.e. use the New keyword, to create an instance. Your problem is the aDayData field. It is an array and therefore a reference type. When you create a MonthData object it has an aDayData variable that does not refer to an object, so you're trying to access the element at index zero of an array that doesn't exist. The rule with arrays is that until you've specified exactly how many elements are in one it doesn't exist. The simple solution is this:
VB.NET:
Dim sMonth As MonthData

sMonth.aDayData = New DayData(31) {} 'This creates an array object.
That's still pretty kludgy though. Exposing an array as a field is basically a bad idea. Exposing a collection through a ReadOnly property is a better bet. That would look something like this:
VB.NET:
Public Structure MonthData

    Public intMonth As Integer
    Private aDayData As ArrayList

    Public ReadOnly Property Days() As ArrayList
        Get
            If Me.aDayData Is Nothing Then
                'Create the collection.
                Me.aDayData = New ArrayList
            End If

            Return Me.aDayData
        End Get
    End Property

End Structure
That would still need some work until I'd consider it completely appropriate.
 
i cant seem to get it to work.. when i try
VB.NET:
Dim sMonth As MonthData

sMonth.aDayData = New DayData(31)
it tells me that DayData doesn't have a constructor

and the second method still gives me null reference =[

I did try Redim and it seemed to work, but I can't seem to redim for the arrays within the other arrays of structure =[
 
That's not the way to create a new array. That is interpreted as invoking a constructor of the DayData type and passing 31 as an argument. You would create a new array like this:
VB.NET:
sMonth.aDayData = New DayData(31 - 1) {}
Proaly a better idea would be to declare a constructor in the MonthData structure that takes a number of days a s an argument. In that constructor you would then create the array:
VB.NET:
Public Sub New(ByVal days As Integer)
    Me.aDayData = New DayData(days - 1) {}
End Sub
You would also be able to use ReDim in the constructor.
 
Back
Top