0

所以我正在为一个我无法理解代码中的错误的类项目工作。我仔细看了看,直到我脸色发青。非常感谢任何有助于理解的帮助。我正在尝试将值加载到数组中,并让该数组以第二种形式打印列表框中的值。

Private Sub displayStd_Click(sender As Object, e As EventArgs) Handles displayStd.Click

    Dim fmtStr As String = "{0,-10}{1,10}{2,15}{3,20}{4,25}"
    Dim form As New Form2()
    form.displayLB.Items.Clear()
    form.displayLB.Items.Add("There are " & Student.Count & " accounts.")
    form.displayLB.Items.Add(String.Format(fmtStr, "ID", "Name", "Score1", "Score2", "Average Score"))


    For Each studentObj As Student In students
        form.displayLB.Items.Add(String.Format(fmtStr, studentObj.ID,
        studentObj.Name, studentObj.Score1, studentObj.Score2, studentObj.CalculateAverage(studentObj.Score1, studentObj.Score2)))
    Next
    form.Show()
    Me.Hide()
End Sub

我越来越:

Student Record.exe 中出现“System.NullReferenceException”类型的未处理异常附加信息:对象引用未设置为对象的实例。

将此突出显示为错误:

form.displayLB.Items.Add(String.Format(fmtStr, studentObj.ID,
            studentObj.Name, studentObj.Score1, studentObj.Score2, studentObj.CalculateAverage(studentObj.Score1, studentObj.Score2)))

计算平均值方法:

Public Function CalculateAverage(score1 As Integer, score2 As Integer)

        Dim sum As Double
        Dim avg As Double


        sum = score1 + score2
        avg = sum / 2

        Return avg


    End Function

学生班级:

Public Class Student

    Private IDVALUE As String
    Private nameValue As String
    Private score1Value As Integer
    Private score2Value As Integer
    Private Shared studentCount As Integer

    Public Sub New(ByVal id As String, ByVal name As String, ByVal score1 As Integer, ByVal score2 As Integer)

        IDVALUE = id
        nameValue = name
        score1Value = score1
        score2Value = score2
    End Sub

    Public Property ID As String


        Get
            Return IDVALUE
        End Get
        Set(value As String)
            IDVALUE = value
        End Set
    End Property

    Public Property Name As String


        Get
            Return nameValue
        End Get
        Set(value As String)
            nameValue = value
        End Set
    End Property

    Public Property Score1 As Integer


        Get
            Return score1Value
        End Get
        Set(value As Integer)
            score1Value = value
        End Set
    End Property

    Public Property Score2 As Integer


        Get
            Return score2Value
        End Get
        Set(value As Integer)
            score2Value = value
        End Set
    End Property

    Public Shared Property Count() As Integer

        Get
            Return studentCount
        End Get
        Set(ByVal value As Integer)
            studentCount = value
        End Set
    End Property

    Public Function CalculateAverage(score1 As Integer, score2 As Integer)

        Dim sum As Double
        Dim avg As Double


        sum = score1 + score2
        avg = sum / 2

        Return avg


    End Function

End Class

Form1类:

Public Class Form1
    Dim students As Student()

    Private Sub addStd_Click(sender As Object, e As EventArgs) Handles addStd.Click
        Dim thisStudent As New Student(idTB.Text, nameTB.Text, CInt(score1TB.Text), CInt(score2TB.Text))

        ReDim Preserve students(Student.Count + 1)
        students(Student.Count + 1) = thisStudent

        idTB.Text = ""
        nameTB.Text = ""
        score1TB.Text = ""
        score2TB.Text = ""
    End Sub
Private Sub displayStd_Click(sender As Object, e As EventArgs) Handles displayStd.Click

        Dim fmtStr As String = "{0,-10}{1,10}{2,15}{3,20}{4,25}"
        Dim form As New Form2()
        form.displayLB.Items.Clear()
        form.displayLB.Items.Add("There are " & Student.Count & " accounts.")
        form.displayLB.Items.Add(String.Format(fmtStr, "ID", "Name", "Score1", "Score2", "Average Score"))


        For Each studentObj As Student In students
            form.displayLB.Items.Add(String.Format(fmtStr, studentObj.ID,
            studentObj.Name, studentObj.Score1, studentObj.Score2, studentObj.CalculateAverage(studentObj.Score1, studentObj.Score2)))
        Next
        form.Show()
        Me.Hide()
    End Sub
End Class
4

2 回答 2

1

您没有初始化 Student 数组。

Dim students As Student()

应该;

Dim students() As Student = New Student() {} 'empty array

重新调暗部分;

ReDim Preserve students(Student.Count)
students(Student.Count - 1) = thisStudent
于 2013-11-14T02:43:34.630 回答
1

我认为你需要使用List(of T)而不是这样的数组:

改变这个:

Dim students As Student()

至:

Dim students As New List(Of Student)

和这个 :

Private Sub addStd_Click(sender As Object, e As EventArgs) Handles addStd.Click
    Dim thisStudent As New Student(idTB.Text, nameTB.Text, CInt(score1TB.Text), CInt(score2TB.Text))

    ReDim Preserve students(Student.Count + 1)
    students(Student.Count + 1) = thisStudent

    idTB.Text = ""
    nameTB.Text = ""
    score1TB.Text = ""
    score2TB.Text = ""
End Sub

对此:

Private Sub addStd_Click(sender As Object, e As EventArgs) Handles addStd.Click
    Dim thisStudent As New Student(idTB.Text, nameTB.Text, CInt(score1TB.Text), CInt(score2TB.Text))

    students.Add(thisStudent)

    idTB.Text = ""
    nameTB.Text = ""
    score1TB.Text = ""
    score2TB.Text = ""
End Sub

现在,如果你想使用 Array 那么我认为你需要overloadNew的'ing,比如:

Public Sub New()

End Sub

然后你现在可以像这样实例化:

Dim students As New Student()

因为你不能在你的情况下实例化,而不需要在你的New(ByVal id As String, ByVal name As String, ByVal score1 As Integer, ByVal score2 As Integer).

于 2013-11-14T02:22:03.543 回答