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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.