NilObjectException

I am trying to create subdirectories in a directory (that is represented by “toDirectory”). the toDirectory is valid. But when I run the code below the first line gives me a NilObjectException exception

if not toDirectory.Child( "Contents" ).Exists then toDirectory.Child( "Contents" ).CreateAsFolder end if

what I am doing wrong?

sb

Does toDirectory exist? If not, that could certainly cause a NilObjectException.

the toDirectory does. the child doesnt. But I am trying to create that directory.

Pop a breakpoint on the “if” line and debug, Then check the toDirectory variable at that point. I had a similar issue that was driving me nuts sometime ago, and I had a typo (reversed two characters) that my eyes just kept missing.

You may breakpoint when toDirectory is initialized etc and step through, I wonder if it goes out of scope etc.

Just a theory.

toDirectory.child(“contents”) is nil ?
trying to check exists on a nil folder item ?

Scott I ran into this recently as I was building a syslog like logger method. First I look to see if my folder is existing or not and if not I create it. It then moves to the file inside of that folder with similar checks. Here is my code and HTH.

  Dim LogFile As FolderItem
  Dim Tos as TextOutputStream
  Dim TimeStamp as New Date
  Dim LogMessage as String
  
  LogFile = SpecialFolder.UserHome
  LogMessage = str(TimeStamp)+"   " + message +Chr(10)+Chr(13)
  
  if LogFile <> nil AND LogFile.Exists AND LogFile.Directory then
    LogFile = SpecialFolder.UserHome.Child(kSystemErrorLogFileDir)
    if LogFile <> nil then
      if not LogFile.Exists then LogFile.CreateAsFolder
      if LogFile.Directory then
        LogFile = LogFile.Child(kSystemErrorLogFile)
        if LogFile <> nil then
          if not LogFile.Directory then
            if LogFile.Exists then
              Tos = TextOutputStream.append(LogFile)
              Tos.Write(LogMessage)
              Tos.Close
            else
              Tos = TextOutputStream.Create(LogFile)
              Tos.Write(LogMessage)
              Tos.Close
            end if
          end if
        end if
      end if
    end if
  end if
  
Exception err as IOException
  SystemErrorLog(1, "IO exception occurred")

Ugh can’t edit :slight_smile: My two Constants from code above.

kSystemErrorLogFile = system_error.log

kSystemErrorLogFileDir = YourFolder

I tried this

dim x as new FolderItem x = toDirectory.Child( "Contents" ) if x <> NIL then if not x.Exists then x.CreateAsFolder end if

and toDirectory is no nil and is a valid folderitem (points to specialfolder.temporary). but starting with the second line (x=todirectory.child(“contents”) ), x is NIL. I dont understand why it is coverting to NIL. yes the “contents” folder/directory doesnt exist. I am trying to create it.

I know how even less hair than I did before.
sb

Q.: toDirectory.exists ? if toDirectory.exists = false then toDirectory.CreateAsFolder
Q.: write access to toDirectory?

[quote=38270:@Joachim Kuno]Q.: toDirectory.exists ? if toDirectory.exists = false then toDirectory.CreateAsFolder
Q.: write access to toDirectory?[/quote]

toDirectory.exists = TRUE (and it is a directory)
have write access to toDirectory. it is specialfolder.temporary (have tried specialfolder.desktop, specialfolder.userhome, etc).

toDirectory is not the issue. the issue is trying to create a subdirectory called “Contents” in that directory. when I do toDirectory.child( “Contents” ).exists it s NIL or toDirectory.child( “Contents” ).createasfolder it is a nilexception. anything I do to reference a new (uncreated) subdirectory of toDirectory, I get some sort of exception (exception will depend on how I am trying to address it). Most nil exceptions.

Could it be permissions related? Anything in x.LastErrorCode?

it is a 0, before I assign x = toDirector.child( “Contents” )
then it is x = NIL so there is no lasterrorcode.

it shouldnt be a permissions issue as toDirectory = specialfolder.temporary is world writeable. and the account that it is running under is an “admin” on the lastest 10.8.x mac.

[quote=38300:@scott boss]it is a 0, before I assign x = toDirector.child( “Contents” )
then it is x = NIL so there is no lasterrorcode.

it shouldnt be a permissions issue as toDirectory = specialfolder.temporary is world writeable. and the account that it is running under is an “admin” on the lastest 10.8.x mac.[/quote]

I have tried other directories like specialfolder.userhome and speciallfolder.desktop with the same luck.

so here is something funny… well to me…

tried writing a directory to the “Desktop” and it failed*.
tried writing a directory to the “Temporary” folder and it failed*.
just now I tried writing to a directory to the “ApplicationData” (~/Library/Application Support) folder and it worked with no issues.

I am just confused. I can write to the ApplicationData folder but not the Temporary folder. I thought the Temporary folder was for writing temp files that you didnt need/want long term. Must be a Mt Lion issue with permissions (not file/directory but gatekeeper related ones).

all I can say is what the hell.

thanks all that have helped out!
sb

  • failed == threw a NilObjectException error.