Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am using a file as a data store. It will allow me to write one word or sentance but if I try to write more than one it gives me my error opening file. Code is as follows:

 

 

VB.NET:

 

Dim strStore As String

Dim fileName As New IO.StreamWriter(File.Open("C:\store.txt", IO.FileMode.Append))

-----------------------------------------------------------------------------------

 

Private Sub cmdStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStore.Click

 

Try

 

strStore = storeText.Text ' Text from text box

fileName.WriteLine(System.Environment.NewLine)

fileName.WriteLine(strStore) ' write to stream

MsgBox(strStore & " has been stored!") ' message has stored

fileName.Flush() ' flush to file

fileName.Close() ' close file

fileName = Nothing

 

Catch ex As Exception

 

MsgBox("Error Opening File!") 'error that is showing on second store attempt

 

End Try

 

End Sub

 

Have a vague idea its something to do with opening and closing the file.

Any ideas of the solution anyone!:confused:

Posted

I have debugged as suggested and the line causing the problem is :

 

fileName.WriteLine(System.Environment.NewLine)

 

The error is:

 

A first chance exception of type 'System.NullReferenceException' occurred in WordMatcher.exe

 

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

 

If this line is commented out it gives the same error, but on the line below:

 

fileName.WriteLine(strStore)

 

Intead of closing the file after the word is added I have put it when the program is exited and it seems to have worked. Would like some explanation on the error, If possible!

Posted

Changing/adding parameters seems to make no difference. It seems that you need to keep the file open, do all your editing, then close it. Doesn't like the file being constantly open and closed at run time it seems. Have learnt a lot so far and at least I have a solution.

 

cheers for al the help everyone!

 

For anyone interested, the solution I found was to add this command on the streamwriter on the exit button.

 

Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

fileName.Close() ' close file

fileName = Nothing

End

End Sub

  • Administrators
Posted
Or if you open the File at the start of the function and close it at the end - just realised you were opening it outside the function (soon as the form is loaded into memory by the look of it) and closing it when the function exits. The next time the function is called the file will be closed.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted

your best bet would be to build up the string you want to write, then use writeline in one go, eg:

strStore = "some stuff"
strStore += Environment.NewLine
strStore += "some more stuff"
fileName.WriteLine(strStore) 

 

also, have you tried using just " Write " rather than " WriteLine "?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...