Works on MAC but nilobject exception on Windows - Why?

Code below works fine on MAC (OSX 10.9.4) but throws a nil object exception on windows - what am I missing?

file = GetFolderItem(FileTypes1.pgn ) If ( file <> Nil ) then tout = TextOutputStream.Create(file) end if

you really use a file type as a file name? Sounds strange.

[quote=118671:@Tony Marino]Code below works fine on MAC (OSX 10.9.4) but throws a nil object exception on windows - what am I missing?

file = GetFolderItem(FileTypes1.pgn ) If ( file <> Nil ) then tout = TextOutputStream.Create(file) end if[/quote]

Your path should be string.

actually specifying a path string alone is not good. Always add a path type, so the runtime knows it’s not just a file name.

But Getfolderitem(“Hello.txt”) should work fine to get a folderitem for such a file in the app folder.

[quote=118671:@Tony Marino] file = GetFolderItem(FileTypes1.pgn )
If ( file <> Nil ) then
tout = TextOutputStream.Create(file)
end if[/quote]

Actually, this is worse than a couple missing quotes. A first glance, loaded into Xojo for Mac :

  • You are using GetFolderItem instead of GetOpenFolderItem
  • FileTypes.png probably
  • file and tout are not dimed

Your code is worthless, and cannot work on Mac OS X more than it does on PC :frowning:

assuming pgn is a mistype of png

on a mac, a file called .png is a valid (but invisible) file.

On windows, maybe not.

But as everyone else says, there is a lot wrong with the code.
Use GetOpenFolderItem to present an open file dialog. (see docs for usage)

Use the filetypes to determine what kind of files you prefer to receive.
Check for an exception when you try to create a file: the user may have chosen a filename that resolves to a CD which you can’t write to.

and so on

Why are we assuming that Tony wants to present a dialog to the user?

For GetOpenFolderItem the parameter with file types would be correct.

So either the function name is correct and parameter wrong or function name is wrong and parameter is correct.

True enough, but there’s no reason to assume the latter.

Because of FileTypes.png. But in my first reply, I did not assume it and suggested GetFolderItem needed a string as argument.

Anyway, whatever he wants to obtain, the OP posted a botched code that is neither here nor there, and when he claims it works fine on Mac, that is simply impossible.

Before assuming if he wants to obtain a result at all, we should wait for a more serious post.

I love this forum.

I only showed the code creating the difference between Windows and MAc. All appropriate dim are done elsewhere. As I said, this code snippet works fine on my MAC.

Will try variations on Windows side to see what the fix may be.

Thanks all for commentary.

Good luck :wink:

Add a breakpoint and check what the file name is resolving to on the if file <> nil line

[quote=118671:@Tony Marino]Code below works fine on MAC (OSX 10.9.4) but throws a nil object exception on windows - what am I missing?

file = GetFolderItem(FileTypes1.pgn ) If ( file <> Nil ) then tout = TextOutputStream.Create(file) end if[/quote]
A FileTypes1.pgn will typically return a string which will contain a “/” character. This is not valid for a file name and will result in a bad file.

Never use a filetype as a file name, you are playing russian roulette.

Bad use of functionality causes bad or unknown behavior. May this page http://documentation.xojo.com/index.php/FileType needs a bit of WHY, WHERE and HOW for each OS.

[quote=118744:@Bob Coleman]A FileTypes1.pgn will typically return a string which will contain a “/” character. This is not valid for a file name and will result in a bad file.

Never use a filetype as a file name, you are playing russian roulette.[/quote]

Thanks Bob. I got it : if he meant FildeTypes.png, the OP is creating a file called images/png, which is a legal file name for Mac, but not for PC. Hence the error he is encountering.

As it stands, though, his Mac code creates the file in the Applications folder, which is very bad form, with a bizarre file name and no extension. Even if it sort of works, it is far from being elegant.

Russian roulette indeed. With a rusty pistol.

And all but one chamber loaded.

Just FYI: pgn is a valid extension (Portable Game Notation)

You asked why it did not work. Using a file type as file name is what got you in trouble with Windows. Not the fact that you used a valid file type name or not.

You wanted to know why it did not work. Now you do.

When you go

file = GetFolderItem(FileTypes1.pgn )

Windows gets as filename “application/x-chess-pgn” which because of the “/” is not an acceptable file name. So your FolderItem ends up nil. If instead you go

file = GetFolderItem("FileTypes1.pgn" )

You keep your very personal programming style, but it works.

FYI: I gave you the substance of the answer in the second reply to your OP.