Error writing to a file.....can't figure it out

kylarr

New member
Joined
Apr 15, 2007
Messages
1
Programming Experience
Beginner
I'm getting an error that says 'An unhandled exception of type "System.NullReferenceException' occurred, Object reference not set to an instance of an object. I pasted the code below any help would greatly be appreciated. also I get this error when the name and age boxes both have input in them so they are not null. Please help!

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] btnWrite_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] btnWrite.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strAge [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] IsNumeric(txtName.Text) [/SIZE][SIZE=2][COLOR=#0000ff]And[/COLOR][/SIZE][SIZE=2] txtName.Text <> "" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]strName = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2]((txtName.Text))
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]MsgBox("Please enter Name with Alphabetic Characters")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IsNumeric(txtAge.Text) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]strAge = [/SIZE][SIZE=2][COLOR=#0000ff]CStr[/COLOR][/SIZE][SIZE=2]((txtAge.Text))
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2]MsgBox("Please enter numeric value")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]strName = (txtName.Text)
strName = (strName.PadRight(25))
strAge = (txtAge.Text)
strAge = (strAge.PadLeft(25))
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]objStreamWriter.WriteLine(strName & strAge)
[/SIZE][SIZE=2][COLOR=#0000ff]Catch
[/COLOR][/SIZE][SIZE=2]MsgBox("Error occurred while writing to file")
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]txtName.Text = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2]txtAge.Text = [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE]
 
my guess is that you're objStreamWriter isn't set to an instance of the StreamWriter Class

objStreamWriter = New StreamWriter("FilePath")

is how you would set it to an instance


here's a suggestion on how i would handle this:
VB.NET:
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
  Dim strName As String = txtName.Text
  Dim strAge As String = txtAge.Text

  If IsNumeric(strName) = True OrElse strName.Trim.Length = 0 Then
    MessageBox.Show("Please enter Name with Alphabetic Characters")
    txtName.Focus
  Else
    If IsNumeric(strAge) = False Then
      MessageBox.Show("Please enter numeric value")
      txtAge.Focus
    Else
      Dim objStreamWriter As New System.IO.StreamWriter("Your File Path")
      Try
        objStreamWriter.WriteLine(strName & New String(Chr(Asc(" ")), 50) & strAge)
      Catch ex As Exception
        MessageBox.Show("Error occurred while writing to file" & Environment.NewLine & ex.Message)
      Finally
        objStreamWriter.Close()
      End Try
      txtName.Text = ""
      txtAge.Text = ""
    End If
  End If
End SUb
 
Back
Top