Object reference error message, please help

ejarju

New member
Joined
Oct 18, 2005
Messages
2
Location
Glasgow
Programming Experience
Beginner
Dear all;
Am new to VB.

I wrote a small program to write some info to a file. But when I run it I am getting an error message:

"Additional information: Object reference not set to an instance of an object"

The error is asociated with a writeLine(1,var). It runs ok the first round but fails with that error above another time.

This is the code: The problem is with the WrIteline command

Thank you in advance
Edi



Dim intWeight As Integer

Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
End Sub

Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub

Private Sub frmDataentry_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtDataEnter.Focus()
lblDate.Text = Today
End Sub

Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim CurrentDate As Date
Dim weight As Integer
Dim txtLine As String
Dim txtFileContent As String = ""
'Dim Myline As New String()
txtLine = ""
CurrentDate = lblDate.Text
weight = txtDataEnter.Text
'txtLine += frmInfoTrack.GetFileContent() + ControlChars.CrLf + "#" + CurrentDate + "#" + "," + Str(weight)
txtFileContent = frmInfoTrack.GetFileContent()
'txtLine += txtFileContent + ControlChars.CrLf + "#" + CurrentDate + "#" + "," + Str(weight)

txtLine += txtFileContent + "#" + CurrentDate + "#" + "," + Str(weight)

MessageBox.Show("This is content of file" + frmInfoTrack.GetFileContent())

MessageBox.Show("this is the content of txtline" + txtLine)

FileOpen(1, frmInfoTrack.AppPath + "Note.ini", OpenMode.Output)

'WriteLine(1, CurrentDate, weight)

Writeline(1,txtLine)
FileClose(1)

txtDataEnter.Text = ""
'Debug.WriteLine(frmInfoTrack.GetFileContent())
MessageBox.Show("This is content after writing" + frmInfoTrack.GetFileContent())

txtLine = ""
Exit Sub
End Sub
End
Class
 
It looks to me like you are coming from a VB6 background. AppPath has been replaced by Application.StartupPath. Also, you should avoid using the "+" operator to concatenate strings. The "&" operator is the string concatenation operator in VB.NET. "+" will produce unexpected behaviour in some circumstances. Also, you are using several Runtime functions. I'd suggest that you get acquainted with the System.IO namespace for file operations. You should create a StreamWriter and use its methods to write to the file. If you're using VB 2005 you can also make use of the My.Computer.FileSystem.OpenTextFileWriter method to create a StreamWriter.
 
Back
Top