Solved

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

Posted on 2004-10-22
347 Views
Last Modified: 2008-01-16

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()
0
Question by:vbcode1
    6 Comments
     
    LVL 14

    Expert Comment

    by:ptakja
    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
     

    Author Comment

    by:vbcode1
    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
     
    LVL 14

    Expert Comment

    by:ptakja
    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
     
    LVL 2

    Expert Comment

    by:GohdanTheMoblin
    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
     

    Author Comment

    by:vbcode1
    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
     
    LVL 14

    Accepted Solution

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now