0

So I'm working on a project for class that I can't understand the error in my code. I have looked over it till I'm blue in the face. Any help in understanding is greatly appreciated. I'm trying to load values into an array and have that array print the values in the list box in a 2nd form.

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

I'm getting:

An unhandled exception of type 'System.NullReferenceException' occurred in Student Record.exe Additional information: Object reference not set to an instance of an object.

highlighting this as an error:

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

CalculateAvg method:

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

Student class:

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 Class:

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
7
  • Did you check the CalculateAverage() method in your Student class? Or could you show that method also here? Commented Nov 14, 2013 at 1:53
  • If you look at your code where you load the students collection you will find it, but you will have to show us or we can't help. Either the students collection is not declared as New or the student objects are not declared with New. Commented Nov 14, 2013 at 1:53
  • Where/how do you declare the collection? How do you fill it? Commented Nov 14, 2013 at 2:06
  • @DonA included the whole form1 class so you can see the whole code. Commented Nov 14, 2013 at 2:15
  • @DonA is there something wrong with how I fill it? Commented Nov 14, 2013 at 2:41

2 Answers 2

1

I think you need to use List(of T) instead of array like this:

Change this:

Dim students As Student()

To:

Dim students As New List(Of Student)

And this one :

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

To this one:

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

Now, if you want to use Array then I think you need to overload your New'ing, like:

Public Sub New()

End Sub

Then you could now instantiate like this:

Dim students As New Student()

Because you could not instantiate in your case without the need to put values on the parameters of your New(ByVal id As String, ByVal name As String, ByVal score1 As Integer, ByVal score2 As Integer).

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry the instructions state it has to be an array.
1

You did not initialize the Student array.

Dim students As Student()

Should be;

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

ReDim part;

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

1 Comment

thanks for helping me fix that, but that doesn't solve my error I'm still getting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.