Web Analytics Made Easy -
StatCounter VB2K5 - "Object reference not set to an instance of an object." Sorting an array. - CodingForum

Announcement

Collapse
No announcement yet.

VB2K5 - "Object reference not set to an instance of an object." Sorting an array.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • VB2K5 - "Object reference not set to an instance of an object." Sorting an array.

    I just got my copy of Visual Studio 2005 and I must say it's a HUGE improvement over 6.0. I'm trying to make a simple little program in Visual Basic 2005, but I'm getting the following runtime error:

    Code:
    Object reference not set to an instance of an object.
    At this line in btnSortItems.Click:

    Code:
                    ListItems(ListItems.Length) = .Items(x - 1)
    I've marked it in my code. . . Can anyone tell me what I need to change? I have a feeling it's something extremely simple that I just don't know since I'm so new to the version.

    Thanks in advance.

    Code:
    Public Class frmStandings
        Dim fmtStr As String = "    {0,3}    {1,-21}    {2,2}    {3,3}    {4,2}    {5,2}    {6,2}    {7,2}    {8,2}    {9,2}    {10,2}    {11,2}    {12,2}    {13,3}    {14,3}"
        Public AllTeamNames(0 To 29) As String
        Public AllTeamInfo(0 To 29, 0 To 7) As Integer
    
        Private Sub frmStandings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            btnAddItem.Width = Me.Width / 5 - 10
            btnEditItem.Left = btnAddItem.Left + btnAddItem.Width + 5
            btnEditItem.Width = Me.Width / 5 - 10
            btnDeleteItem.Left = btnEditItem.Left + btnEditItem.Width + 5
            btnDeleteItem.Width = Me.Width / 5 - 10
            btnSortItems.Left = btnDeleteItem.Left + btnDeleteItem.Width + 5
            btnSortItems.Width = Me.Width / 5 - 10
            btnExit.Left = btnSortItems.Left + btnSortItems.Width + 5
            btnExit.Width = Me.Width / 5 - 10
    
            lstStandings.Left = 0
            lstStandings.Top = 47
            lstStandings.Width = Me.Width
            lstStandings.Height = Me.Height - 47
            lstStandings.Items.Add(String.Format( _
                fmtStr, _
                "Num", "Team", "GP", "PTS", "W", _
                "L", "OTL", "HW", "HL", "HOL", "AW", _
                "AL", "AOL", "GF", "GA" _
                ) _
            )
    
            If Not IO.File.Exists("standings.txt") Then
                Dim sw As IO.StreamWriter = IO.File.CreateText("standings.txt")
                sw.Close()
            End If
    
            Dim sr As IO.StreamReader = IO.File.OpenText("standings.txt")
    
            Dim Line As String = "", TeamInfo(0 To 8) As String
            Dim Counter As Integer = 0
            While Not sr.EndOfStream
                Line = sr.ReadLine
                Counter += 1
                Line.TrimEnd()
                TeamInfo = System.Text.RegularExpressions.Regex.Split(Line, "\|\%\^\%\|")
                lstStandings.Items.Add(String.Format( _
                    fmtStr, _
                    Counter, _
                    TeamInfo(0), _
                    CStr(CInt(TeamInfo(1)) + CInt(TeamInfo(2)) + CInt(TeamInfo(3)) + CInt(TeamInfo(4)) + CInt(TeamInfo(5)) + CInt(TeamInfo(6))), _
                    CStr((CInt(TeamInfo(1)) + CInt(TeamInfo(4))) * 2 + CInt(TeamInfo(3)) + CInt(TeamInfo(6))), _
                    CStr(CInt(TeamInfo(1)) + CInt(TeamInfo(4))), _
                    CStr(CInt(TeamInfo(2)) + CInt(TeamInfo(5))), _
                    CStr(CInt(TeamInfo(3)) + CInt(TeamInfo(6))), _
                    TeamInfo(1), _
                    TeamInfo(2), _
                    TeamInfo(3), _
                    TeamInfo(4), _
                    TeamInfo(5), _
                    TeamInfo(6), _
                    TeamInfo(7), _
                    TeamInfo(8) _
                    ) _
                )
                AllTeamNames(Counter - 1) = TeamInfo(0)
                AllTeamInfo(Counter - 1, 0) = TeamInfo(1)
                AllTeamInfo(Counter - 1, 1) = TeamInfo(2)
                AllTeamInfo(Counter - 1, 2) = TeamInfo(3)
                AllTeamInfo(Counter - 1, 3) = TeamInfo(4)
                AllTeamInfo(Counter - 1, 4) = TeamInfo(5)
                AllTeamInfo(Counter - 1, 5) = TeamInfo(6)
                AllTeamInfo(Counter - 1, 6) = TeamInfo(7)
                AllTeamInfo(Counter - 1, 7) = TeamInfo(8)
            End While
            sr.Close()
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
            End
        End Sub
    
        Private Sub frmStandings_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
            btnAddItem.Width = Me.Width / 5 - 10
            btnEditItem.Left = btnAddItem.Left + btnAddItem.Width + 5
            btnEditItem.Width = Me.Width / 5 - 10
            btnDeleteItem.Left = btnEditItem.Left + btnEditItem.Width + 5
            btnDeleteItem.Width = Me.Width / 5 - 10
            btnSortItems.Left = btnDeleteItem.Left + btnDeleteItem.Width + 5
            btnSortItems.Width = Me.Width / 5 - 10
            btnExit.Left = btnSortItems.Left + btnSortItems.Width + 5
            btnExit.Width = Me.Width / 5 - 10
            lstStandings.Width = Me.ClientSize.Width
            lstStandings.Height = Me.ClientSize.Height - lstStandings.Top
        End Sub
    
        Private Sub btnEditItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEditItem.Click
            With lstStandings
                If .SelectedIndex = -1 Then
                    MsgBox("Please choose a team to edit.")
                    Exit Sub
                End If
    
                SelIndex = .SelectedIndex - 1
                frmEdit.Show()
            End With
        End Sub
    
        Private Sub btnSortItems_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSortItems.Click
            Dim x As Integer, ListItems() As String = Nothing
            With lstStandings
                For x = 2 To .Items.Count
                    MsgBox(CStr(x))
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' This is where the error occurs
                    ListItems(ListItems.Length) = .Items(x - 1)
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                Next x
            End With
    
            Array.Sort(ListItems, sortStandings)
    
            lstStandings.Items.Clear()
    
            For x = 1 To ListItems.Length
                lstStandings.Items.Add(ListItems(x))
            Next x
        End Sub
    
        Public Shared Function sortStandings() As IComparer
            Return CType(New sortStandingsClass, IComparer)
        End Function
    End Class
    
    Public Class sortStandingsClass : Implements IComparer
    
        Public Function Compare(ByVal a As Object, ByVal b As Object) As Integer Implements System.Collections.IComparer.Compare
            Dim a1 As String = CStr(a)
            Dim b1 As String = CStr(b)
    
            a1 = a1.Trim
            b1 = b1.Trim
    
            Dim aSplit As Array = System.Text.RegularExpressions.Regex.Split(a1, "\s+")
            Dim bSplit As Array = System.Text.RegularExpressions.Regex.Split(b1, "\s+")
    
            Dim aPts As Integer = CInt(aSplit(3))
            Dim bPts As Integer = CInt(bSplit(3))
    
            If aPts > bPts Then
                Return 1
            ElseIf bPts > aPts Then
                Return -1
            Else
                Return 0
            End If
        End Function
    End Class
    Last edited by KEVaholic00; Oct 9, 2006, 06:16 PM.

  • #2
    It is because you are setting it to nothing when you declare it. There is no instance of the string object in your array. Lose the '= Nothing' and give it a whirl.
    OracleGuy

    Comment


    • #3
      I tried that and got the same result.

      Comment

      Working...
      X