Question Null Reference Exception

CaptainSolo24

New member
Joined
May 28, 2009
Messages
1
Location
PA
Programming Experience
3-5
This is my first vb.net project in 2 years and coming from the java and c++ world it is so much easier but some things just don't work the some as you already know. My problem is I have a class called Hteam and i have an array of Hteam as public so I can use them throughout my program but the issue lies where I try to set the team name it gives the NullReference Exception
but im not sure why? I use Inputboxes in get data from user.
I was able do this in java but not vb.net!



VB.NET:
Imports System.Globalization
Public Class Form1
    Public maps() As String = {"Amplified", "Assembly", "Avalanche", "Blackout", "Cold Storage", "Construct", "Epitaph", "Foundry", "Ghost Town", "Guardian", "High Ground", "Isolation", _
"Last Resort", "Lockdown", "Narrows", "Onslaught", "Orbital", "Rat's Nest", "Sandbox", "Sandtrap", "Snowbound", "Standoff", "The Pit", "Valhalla", "OTHER"}
    Public teams() As Hteam
    Public numTeam As Integer
    Public tourType As Integer

    Private Sub startB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startB.Click
        Dim name As String = " "
        Dim num As Integer = 0
        Dim s As String
        Dim n As Integer = 0


        s = InputBox("Enter the number teams:", "enter", "0")

        Integer.TryParse(s, numTeam)


        Do While n < numTeam
            teams(n).setTeam(0, 0, 0, 0, "") 'Error here
            n += 1
        Loop

        Do While num < numTeam
            name = InputBox("Enter a team name:", "enter", "Team Name")


            teams(num).setTeamname(name) 'Error here
            num += 1
        Loop

    End Sub


Hteam Class

VB.NET:
Public Class Hteam
    Private teamName As String
    Private wins As Integer
    Private numMembers As Integer
    REM private members[] teamMembers = new members[numMembers];
    Private losses As Integer
    Private ties As Integer

    Public Sub New()


        setTeam(0, 0, 0, 1, "")

    End Sub

    Public Sub New(ByVal w As Integer, ByVal l As Integer, ByVal t As Integer, ByVal nm As Integer, ByVal tname As String)


        setTeam(w, l, t, nm, tname)

    End Sub

    Public Sub setTeam(ByVal w As Integer, ByVal l As Integer, ByVal t As Integer, ByVal nm As Integer, ByVal tname As String)

        teamName = tname
        If ((nm > 0) And (nm < 5)) Then
            numMembers = nm
        Else
            numMembers = 1
        End If
        If (w >= 0) Then
            wins = w
        Else
            wins = 0
        End If
        If (l >= 0) Then
            losses = l
        Else
            losses = 0
        End If
        If (t >= 0) Then
            ties = t
        Else
            ties = 0
        End If
    End Sub
    '	public void setMembers()
    '	{
    '		for(int i = 0; i < numMembers;i++)
    '		{
    '			teamMembers[i].setName("Mem");
    '		}	

    '	}

    Public Sub setTeamname(ByVal tn As String)

        teamName = tn

    End Sub
    Public Sub setNummem(ByVal n As Integer)

        numMembers = n
    End Sub

    Public Sub addWin()

        wins = wins + 1
    End Sub

    Public Sub addLoss()

        losses = losses + 1
    End Sub

    Public Sub addTie()

        ties = ties + 1
    End Sub

    '	Public Property getMembername(int n)

    '	return teamMembers[n].getName();

    Public Property getWins() As Integer
        Get

            Return wins
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property getLosses() As Integer
        Get

            Return losses

        End Get
        Set(ByVal value As Integer)

        End Set
    End Property

    Public Property getTies() As Integer
        Get

            Return ties

        End Get
        Set(ByVal value As Integer)

        End Set
    End Property
    Public Property getTeamname() As String
        Get

            Return teamName
        End Get
        Set(ByVal value As String)

        End Set
    End Property
    Public Property getNummem() As Integer
        Get
            Return numMembers
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property


End Class
 
First up, please post in the most appropriate forum for the topic, not just the first one you come to. Moved.

As for the question, it's nothing to do with your Hteam class. When you got the null reference exception did you check what reference was null? It looks to me like your first problem is that you haven't actually created an array. Arrays are instances of the Array class and to create one you have to specify its upper bound. You haven't done that anywhere. You've declare an Hteam() variable:
VB.NET:
Public teams() As Hteam
but nowhere have you actually create an Hteam() object by specifying an upper bound. As such your teams variable is Nothing, i.e. a null reference.

Once you know how many teams there are supposed to be you need to use that number to create an array of that size, which you can do with the ReDim statement or the New keyword.

Once that's done, you still need to create your Hteam objects. Creating an egg carton doesn't create any eggs. You then need to use the Hteam constructor once for each element to create an Hteam object and assign it to the element. Otherwise each element is Nothing, i.e. a null reference.
 
Back
Top