New topic: 

FolderItems, NilObjectExceptions, Completely Lost

<http://forums.realsoftware.com/viewtopic.php?t=31381>

       Page 1 of 1
   [ 6 posts ]                 Previous topic | Next topic         Author  
Message       applesource           Post subject: FolderItems, 
NilObjectExceptions, Completely LostPosted: Wed Dec 02, 2009 3:09 pm            
                   
Joined: Thu Aug 06, 2009 2:25 pm
Posts: 214
Location: Oregon              So, last week in an effort to prevent random 
NilObjectExceptions that I cannot explain for the life of me, I changed all 
references to folderitems from my old method, 
f=SpecialFolder.Preferences.child("folder1:folder2:folder3") to the new, 
recommended method 
f=SpecialFolder.Preferences.child("folder1").child("folder2").child("folder3"). 
Ever since this change the error reports I'm getting back from my customers 
have more than tripled.

What's going on here, and what's the proper reliable way to reference 
folderitems with REALbasic? Been using it for almost 15 years now and I still 
cannot seem to escape this silly issue. I develop for Mac OS X, by the way. 
Getting very frustrated and may switch to xCode for the sole purpose of being 
able to develop an application that handles data reliably.

Can anyone provide some insight into this? I personally find it hard to believe 
at this point that REALbasic is capable of creating rock-solid data based 
applications (without using SQL databases - maybe that improves things, I don't 
know). I may be wrong - what is your take?     
_________________
AppleSource Software
http://www.applesource.biz  
                            Top                mjh           Post subject: Re: 
FolderItems, NilObjectExceptions, Completely LostPosted: Wed Dec 02, 2009 3:37 
pm                        
Joined: Sun Feb 19, 2006 3:10 pm
Posts: 467
Location: Hamburg, Germany              Basically you can create a FolderItem 
that refers to an existing file or folder, or you can create a FolderItem 
referring to a file or folder that does not exist but would reside with a 
folder that does exist. When you are chaining several calls to the Child method 
(which is definitely not recommended!), you run the risk that one of those 
calls will yield a FolderItem that doesn’t exist, and if you call Child on 
that again, you get Nil, leading to a NilObjectException with the third call of 
the Child method.

The only safe method is not to chain Child calls but to execute them one after 
the other, checking whether the resulting FolderItem isn’t Nil and exists 
after each call. When you hit upon a non-existing folder, that’s an excellent 
place to create that folder and continue.     
_________________
Michael J. Hußmann 
http://digicam-experts.de  
                            Top               ZULU           Post subject: Re: 
FolderItems, NilObjectExceptions, Completely LostPosted: Wed Dec 02, 2009 3:41 
pm                        
Joined: Mon Jul 31, 2006 1:44 am
Posts: 1023              Yes, you doing this wrong:
f=SpecialFolder.Preferences.child("folder1").child("folder2").child("folder3")


You MUST do it like this to be safe ...

Code:Dim f as folderitem = SpecialFolder.Preferences

if f <> nil and f.exist then
  f = f.child("folder1")
  if f <> nil and f.exists then
  f = f.child("folder2")
  if f <> nil and f.exists then
  f = f.child("myPrefs.txt")
  if f <> nil then // only nil check if you should create/Write a new file
    
    //Create your file here
    
    
  end if // Prefs file <> nil
  end if // Folder 2
  end if // Folder 1
end if // Preferences


     
_________________
------------------------------------  
                            Top               applesource           Post 
subject: Re: FolderItems, NilObjectExceptions, Completely LostPosted: Wed Dec 
02, 2009 3:48 pm                               
Joined: Thu Aug 06, 2009 2:25 pm
Posts: 214
Location: Oregon              Okay, thank you for the information. I will make 
those changes and see if it improves things.     
_________________
AppleSource Software
http://www.applesource.biz  
                            Top                AaronAndrewHunt           Post 
subject: Re: FolderItems, NilObjectExceptions, Completely LostPosted: Wed Dec 
02, 2009 4:16 pm                        
Joined: Sat Jul 14, 2007 2:16 pm
Posts: 212              This problem once nearly drove me insane. Help from 
this forum led me to the following solution. Add this function to a global 
module:

Code:Function TestSubFolder(fRoot as folderitem, ParamArray ChildNames as 
string) As folderitem
  dim j as integer
  dim f as FolderItem
  dim s as string
  
  if fRoot = Nil then f = Volume(0) else f = fRoot
  
  for j = 0 to Ubound(ChildNames)
  s = ChildNames(j)
  if f = Nil or not f.Exists then return Nil
  f = f.Child(s)
  next
  
  Return f
End Function



for your code

Code:
f=SpecialFolder.Preferences.child("folder1").child("folder2").child("folder3")



Use instead:

Code:
f = TestSubFolder(SpecialFolder.Preferences, "folder1", "folder2", "folder3")
if f <> Nil and f.exists then
  'do whatever
end if



It will get the item without throwing an exception, return the folderitem if 
it's there, or return Nil if it's not; the desired behavior.   
                            Top               applesource           Post 
subject: Re: FolderItems, NilObjectExceptions, Completely LostPosted: Wed Dec 
02, 2009 4:29 pm                               
Joined: Thu Aug 06, 2009 2:25 pm
Posts: 214
Location: Oregon              Excellent tip. Globally implementing the new 
method now. Will report back in a week or so if the errors have reduced. If 
anyone else has any more thoughts on this, I'd love to hear it.     
_________________
AppleSource Software
http://www.applesource.biz  
                            Top            Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 6 posts ]     
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to