NilObject problem

Are you confirming that only for a TextOutputStream the check for NIL is not working anymore?
And if yes, what means “won’t work anymore” ? Is it always NIL?

Just to make sure i understand it correctly:
The Sample Code taken from the Docs:

Var file As FolderItem = FolderItem.ShowSaveFileDialog("", "MyInfo.txt")
If file <> Nil Then
  Var fileStream As TextOutputStream
  fileStream = TextOutputStream.Create(file)
  fileStream.WriteLine(NameField.Value)
  fileStream.WriteLine(AddressField.Value)
  fileStream.WriteLine(PhoneField.Value)
  fileStream.Close
End If

now should be more like this?:

Var file As FolderItem = FolderItem.ShowSaveFileDialog("", "MyInfo.txt")
If file <> Nil Then
  Var fileStream As TextOutputStream
  Try
    fileStream = TextOutputStream.Create(file)
    fileStream.WriteLine(NameField.Value)
    fileStream.WriteLine(AddressField.Value)
    fileStream.WriteLine(PhoneField.Value)
    fileStream.Close
  Catch err As IOException
    // Do stuff here...
  End Try
End If

Trouble with that is then not knowing which statement caused the exception, although I suppose the error code might tell you.

Running in the debugger will still stop at the fault.

Wouldn’t you also need to catch NilObjectException? What about OutOfMemoryException?

Switching the Xojo framework from error codes to exceptions would have been a much better if Xojo supported checked exceptions.

Without them its a complete guess on what calls can raise exceptions and what exceptions can be raised. What is even worse is that Xojo could change it between versions and we would never know.

You catch exceptions you know how to handle as their type:

try
  var toStream as TextOutputStream = TextOutputStream.Open(tfMyDoc)
  toStream.Write(tsData)
  toStream.Close

catch ex as NilObjectException
  // More than likely the FolderItem
  MessageBox("Something was nil.")

catch ex as OutOfMemoryException
  MessageBox("Out of memory! " + ex.Message)

end try

If you’re going to do something you shouldn’t like catch all exceptions, you can use Introspection to reach which one it is. The code for that is on the documentation page for Introspection - it’s the main example.

If you’d like to continue this discussion in a new thread, I’d be happy to help answer questions about exceptions :slight_smile:

1 Like

Not in my example. Because we check for file<>nil right before we create the TextOutputStream and i gues NameField, AddressField and PhoneField are TextFields within scope and their Values can’t be Nil. And @Greg_O_Lone wrote:

But as @Tim_Parnell wrote, you could add a

catch err as NilObjectException

and

catch err as OutOfMemoryException

and so on, before the Try End to be safe. :slight_smile:

If you have already checked for <> nil, there should be no way that a NilObjectException can occur, or why have the test?
If testing for nil doesn’t work, that’s a serious bug.
What am I missing here?

If TextOutputStream.Open(tfMyDoc) raised a NilObjectException it would not be caught by checking if toStream is Nil.

1 Like

Me just answering @kevin_g’s question :wink:

:slight_smile:

1 Like