text file folderitem problem

Hi, I’ve just returned to xojo after a four-year gap and am having trouble understanding why the following code doesn’t work any more. I have a simple text file called “mytext.txt” inside folder2, inside folder1, which is in the same folder as the application I’m building. It works OK right up to the last item .child(“mytext.txt”) when I get a nil object exception. I’ve tried calling it just “mytext”, tried turning off “hide file extensions” in Windows 10 and tried making the file openable by xojo. What am I doing wrong?

[code]dim f as FolderItem
dim i as TextInputStream
dim linesArr(-1) as text
dim t as text

f = GetFolderItem("").Child(“folder1”).Child(“folder2”).Child(“mytext.txt”)[/code]

I wouldn’t bother trying to get this to work. To develop an app that’s safe to deliver to any user and offers reliable FolderItem access you need to forget that GetFolderItem ever existed.

You get a nil FolderItem when you use Child with a FolderItem that doesn’t exist. Using any function or property of the now nil FolderItem will raise a NilObjectException. So this code isn’t going where you think it’s going (one of the biggest reasons not to use GetFolderItem.)

Use an OpenDialog to ask the user which files to open, and a SaveAsDialog to ask the user where to save files.

+1 to Tims comments, plus:

If this file is one of ‘yours’, you should normally keep it in
specialfolder.applicationdata.child(“MyApp”) folder
and refer to it as
specialfolder.applicationdata.child(“MyApp”) .child(“mytext.txt”)

The most likely reason you are having trouble right now is that ‘next to my app’ isn’t the same place when running the release app, as it is when running the debug version.
Using specialfolder solves that too.

How do you know which .Child have a problem ?
folder1, folder2, or mytext.txt ?

Error checking and doing what have to be done to avoid Exceptions is an important part of a developer time (and add me to what Tim and Jeff wrote above).

At last, reading the documentation is mandatory.

So, read the documentation for TextInputStream too (why did they use Try in the documentation ?) .

Thanks Tim, you seem to have a down on getfolderitem, although as far as I know it’s not deprecated. My users will not need to save any files and I don’t want them to have to worry about where necessary files are to be found. The files will be data tables and changing those from time to time will be much easier than rewriting the app.

Thanks Jeff, it’s just a personal preference of mine to keep apps and app data grouped together in one folder rather than in separate places. However, your comment about runtime and debug differences jogged my memory and led me to the answer. Many thanks. Since xojo creates some kind of extra debug animal:

#if DebugBuild then f=GetFolderItem("").Parent.Child("folder1").Child("folder2").Child("mytext.txt") #else f=GetFolderItem("").Child("folder1").Child("folder2").Child("mytext.txt") #endif

Emile, I found the tone of your lecture insulting and unpleasant. What makes you assume I hadn’t read the documentation without finding a solution before asking what I thought was a simple question on the Getting Started thread? I said that I was returning to xojo after a gap of four years and I chose this thread deliberately. It is for beginners with simple problems that seem difficult to them. If you take that tone with everybody you will make people feel small, deter them from asking questions again and may scare them off using xojo altogether.

Long toes? You are aware he did take time to help you? Maybe next time we just ignore you questions?

GetFolderItem causes problems when used in production. It may work temporarily, but GetFolderItem is usually the source of issues when your app stops functioning once it leaves your machine.

Storing user data next to the app violates Apple’s file system rules, meaning your app won’t work at all in many cases.

For example, this:

Other example, you do not use Try.

Sorry you took my answer bad. Think back to your question after looking at TextInputStream / TextOutputStream.

And, of course, I wanted to help.

Usually I do not read the channel’s name, only if I have some doubts. But from new comers to old timers (people with Xojo experience) are meant to read the LR. I read it everyday (sometimes I read it bad, skip paragraphs, wrongly understand, etc., but I read it).

I nearly forget: how do you debug such sentence:

Like me, you will decompose that line into four lines:
line 1: f = GetFolderItem("").Parent
Line 2: f = f.Child("folder1")
Line 3: f = f.Child("folder2")
Line 4: f = f.Child("mytext.txt")

If in line 1, GetFolderItem(“”) returns the Volume Root, .Parent will return Nil. (I forgot that in my prior answer.

So, if there are some troubles, you will know in what line(s). Yes, I do not love at all this way of programming, but this is the reason why.

But, you are free to do as you whish.

In the next answer you were reading, skip what you consider as bad tone, and concentrate on the development part: you may get a good advice (or not) to follow.