System.NullReferenceException --- Object reference not set to an instance of an object


I get this error:  (An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication2.exe
Additional information: Object reference not set to an instance of an object.), when I run this code.  

I also cannot get this code to write the filestream information in the text file I am creating.  Can anyone tell me what is wrong and how to fix it?


  Dim Students As ArrayList
        Dim Student As StudentInformation
        Dim sPath As String = "C:\Documents and Settings\Administrator.DANDK\My Documents\Visual Studio Projects\WindowsApplication2\WindowsApplication2\StudentInformation.txt"
        Dim StudentStreamWriter As StreamWriter
        File.Delete(sPath)
        Dim StudentFileStream As New FileStream(sPath, FileMode.CreateNew, FileAccess.Write)
        StudentStreamWriter = New StreamWriter(StudentFileStream)

*******************************************************
error references here at this point
*******************************************************
       
**  For Each Student In Students
            StudentStreamWriter.WriteLine(Student.StudentName & "," & Student.StudentAddress & "," & Student.StudentCity & "," & Student.StudentState & "," & Student.StudentZip & "," & Student.StudentPhone & "," & Student.StudentCountry)
        Next
        StudentFileStream.Close()
        StudentStreamWriter.Close()
vbcode1Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ptakjaConnect With a Mentor Commented:
Try this:

   Private Sub btnLoadStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadStudent.Click

        Dim Students As New ArrayList
        Dim Student As StudentInformation
        Dim sPath As String = "C:\Documents and Settings\Administrator.DANDK\My Documents\Visual Studio Projects\WindowsApplication2\WindowsApplication2\Students.txt"

       Dim oFile as System.IO.File
       Dim oWrite as System.IO.StreamWriter

       oWrite.WriteLine(“Write a line to the file”)

       oWrite.Close  ' Closes streamwriter.

        Dim oFile As File
        Dim StudentStreamWriter As StreamWriter = oFile.CreateText(sPath)
             
        For Each Student In Students
            Dim txt as String
            With Student
                  txt = String.Format("{0},{1},{2},{3},{4},{5},{6}, {7}",  _
                  .StudentName, .StudentAddress, .StudentCity, .StudentState, .StudentZip, .StudentPhone, .StudentCountry)
            End WIth
            StudentStreamWriter.WriteLine(txt)
        Next
        StudentStreamWriter.Close()
    End Sub

Also, just an observation on coding style, when you create a structure or class, you should consider naming your class members with shorter names that don't duplicate the class name.

For example, you have this Students structure or class object:

Students.StudentName
Students.StudentAddress
Students.Studentxxxxxxx

I can deduce just from the name of the class that you are talking about students, so it is redundant to have the members prefixed with Student. A better approach would be this:

Students.Name
Students.Address
Students.City
etc...

For what it's worth...
0
 
ptakjaCommented:
The problem is that Students is declared as an ArrayList object, but all you did was give it a type. You never actually created it.

In addition, the code you posted doesn't fill Students with anything.

For this to work you would need to change the line:

Dim Students As NEW ArrayList

Now you need to fill Students

Students.Add("Student 1")
Students.Add("Student 2")
etc...

NOW your For Each loop will work.
0
 
vbcode1Author Commented:
Ptakja,

Here is more of the code I am using including what was previously posted.  I got the exception error fixed, but I have not been able to get the code to write the the text  in the text file I create.

    Dim Students As New ArrayList

    Private Sub btnUpdateStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdateStudent.Click

        'Declare Student under StudentInformation structure
        'Declare text box variables for students
        Dim Student As StudentInformation
        Student.StudentName = StudentName.Text
        Student.StudentAddress = StudentAddress.Text
        Student.StudentCity = StudentCity.Text
        Student.StudentState = StudentState.Text
        Student.StudentZip = StudentZip.Text
        Student.StudentPhone = StudentPhone.Text
        Student.StudentCountry = StudentCountry.Text
        'Add Student information to array
        Students.Add(Student)
        StudentName.Text = ""
        StudentAddress.Text = ""
        StudentCity.Text = ""
        StudentState.Text = ""
        StudentZip.Text = ""
        StudentPhone.Text = ""
        StudentCountry.Text = ""
        ListBox1.Items.Add(Student.StudentName & "  " & Student.StudentAddress & "  " & Student.StudentCity & ", " & Student.StudentState & "  " & Student.StudentZip & "  " & Student.StudentPhone & "  " & Student.StudentCountry)
    End Sub
*******************************************************************

    Private Sub btnLoadStudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadStudent.Click

        Dim Students As New ArrayList
        Dim Student As StudentInformation
        Dim sPath As String = "C:\Documents and Settings\Administrator.DANDK\My Documents\Visual Studio Projects\WindowsApplication2\WindowsApplication2\Students.txt"
        Dim StudentStreamWriter As StreamWriter
        File.Delete(sPath)
        Dim StudentFileStream As New FileStream(sPath, FileMode.CreateNew, FileAccess.Write)
        StudentStreamWriter = New StreamWriter(StudentFileStream)
        For Each Student In Students
            StudentStreamWriter.WriteLine(Student.StudentName & "," & Student.StudentAddress & "," & Student.StudentCity & "," & Student.StudentState & "," & Student.StudentZip & "," & Student.StudentPhone & "," & Student.StudentCountry)

        Next
        StudentFileStream.Close()
        'StudentStreamWriter.Close()
    End Sub


    Private Sub CloseLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseLoad.Click
        Me.Close()
    End Sub
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
ptakjaCommented:
I found this example on the web...

Dim oFile as System.IO.File
Dim oWrite as System.IO.StreamWriter
oWrite = oFile.CreateText(“C:\sample.txt”)

oWrite.WriteLine(“Write a line to the file”)

oWrite.Close  ' Closes streamwriter.
0
 
GohdanTheMoblinCommented:
The problem I see is you are declaring

Dim Students As New ArrayList

but never adding any students to the list.  So when you tell your code to write each student in the list to the file, it is writing all 0 of them.
0
 
vbcode1Author Commented:
Can you give an example?  I added the above array list:

Dim Student As StudentInformation
        Student.StudentName = StudentName.Text
        Student.StudentAddress = StudentAddress.Text
        Student.StudentCity = StudentCity.Text
        Student.StudentState = StudentState.Text
        Student.StudentZip = StudentZip.Text
        Student.StudentPhone = StudentPhone.Text
        Student.StudentCountry = StudentCountry.Text

Beneath my declaration for the new arraylist.  It still does not write the array information to the file.
0
All Courses

From novice to tech pro — start learning today.