Does textoutputstream.create delete a file if it already exists, or does it throw an error?
f = SpecialFolder.Preferences.Child(“com.app.plist”)
try
tos = TextOutputStream.Create(f)
Catch err As IOException
msgbox ( ioexception.message ) //This should work I think, but doesn’t.
End Try
I can’t get Xojo to recognize an error message (ioexception.message), even though the autocomplete is saying it is valid syntax
I’m not sure if create deletes the file if it already exists so I can’t help you with that, but your problem with the second part should be that you are catching err as IOException. Therefore you don’t display the class message, but instead the err message. Using
f = SpecialFolder.Preferences.Child("com.app.plist")
try
tos = TextOutputStream.Create(f)
Catch err As IOException
msgbox (err.message ) //switch ioexception to err because err is the instance of ioexception caught by the catch block
End Try
will create a new file assuming that “f” is a valid folder item, and the app has permission to write to that location.
Does it “delete” the existing file? Possibly… but since the request is CREATE… that is what would happen… delete or not, the existing contents (if any) are destroyed.
So… pre-existing or not… as long as valid folderitem and permission… a file will be created…
I am going nuts. Can you see where I am off in my code? It would write the file and I am not getting an error message (it is still blank).
dim f as folderitem #if TargetMacOS
f = SpecialFolder.Preferences.Child(“com.teknia.flashworks.plist”) #else
f = SpecialFolder.Preferences.Child(“com.teknia.flashworks.xml”) #endif
If f <> Nil Then
Try
Dim tos as TextOutputStream = TextOutputStream.Create(f) //I get a nilobject error right here
tos.Write "Stuff stuff stuff."
tos.Close
Catch err As IOException
msgbox ("Error writing the preferences file." + chr(13) + chr(13) + "(" + err.message + ")" ) //err.message has no content
End Try
Should it be giving you an error? Is the message being shown as though there is an error but there is no error message supplied? It’s possible that there is no error message for the error that is occurring, if an error is occurring.
When I run the line, Dim tos as TextOutputStream, Xojo says it is a nilobject. I don’t think it should throw an error, but it does. I can’t see anything wrong in the code.
You need to create a new instance of TextOutputStream when you declare it. Add new to your declaration and it should fix that nilobjectexception. Like this:
No, Create is a shared method. It will work whether the object is nil or not. That is not what’s generating the exception. Also, your Catch shouldn’t be triggered, since you’re not catching a nilobjectexception. It should skip past it.
The only thing that could possibly trigger an exception on that line is “f”. Recheck your code.
Write permission may not be available to the user. Dave has already mentioned that above.
Your code works fine when used on Windows 7, Xojo 3.2 without any error and the resulting file “com.teknia.flashworks.xml” does contain the text “Stuff stuff stuff.”.
Okay. Here is a weird thing. When I compile the app, it works. When I run it in the development mode, it generates the error. No it runs now. I didn’t do anything. Hmmm. Thanks for all your help. For whatever reason, it is working.
Just a guess here… seeing as how it is not consistent.
Do you have some backup software? anti-virus? or something that may lock the drive for a few seconds?
and you just HAPPEN to hit that? just grasping straws here for you.
I am grasping too. Thanks. No virus software. I just can’t a consistent behavior. Sometimes the file is created, sometimes it is created with garbled content, and other times it seems nothing has happened. Xojo folks (that’s you, Jason), could this be an issue in Xojo?
Has your app got a bundle id of com.teknia.flashworks ?
And your trying to create com.teknia.flashworks.plist ?
Name the file you’re trying to create something else just for fun (like comtekniaflashworks.plist)