Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-22
8
Medium Priority
?
376 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
Comment
Question by:vbcode1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 14

Expert Comment

by:ptakja
ID: 12386161
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
ID: 12386241
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
ID: 12388475
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:GohdanTheMoblin
ID: 12402965
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
ID: 12406999
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:
ptakja earned 200 total points
ID: 12410040
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 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