NilObjectException error

Hello guys,

          I have a code FOR MAC OS that searches a folder and upload a picture it on a canvas, what is happening is that the image exists and the path is correct but some machines (Mac) works fine and another is giving a NilObjectException error, someone could help me with this because I am unsure what to do! 

Any help is welcome. . .

post a snippet of code to give us an idea what/how you are trying to do it

Hi Dave, my code is:

Dim f as FolderItem
Dim p, pic as Picture
Dim factor As Double
Dim maxWidth, maxHeight As Integer
dim filepath as string

maxWidth = Window7.canvas1.width
maxHeight = Window7.canvas1.height

f=GetFolderItem(SpecialFolder.library.child(“Imageok”).child(“Temp.jpg”).AbsolutePath)

If f.exists then
p=f.openaspicture
factor = Min( maxWidth / p.Width, maxHeight / p.Height )
factor = Min( factor, 1 ) // (don’t scale it up if it’s too small!)
factor= factor * 1
pic = NewPicture( p.Width * factor, p.Height * factor, 32 )
pic.graphics.DrawPicture p, 0,0,pic.width,pic.height, 0,0,p.width,p.height
Window7.Canvas1.Backdrop=pic

dim mypic as Picture

End if

Window7.ShowModalWithin(self)

Exception err
If err IsA NilObjectException then

end if

First off… two things wrong with this line

  1. ABSOLUTEPATH is deprecated, use NATIVEPATH instead…
  2. no need to be that complex… this works much easier
f=SpecialFolder.library.child("Imageok").child("Temp.jpg")

Not sure where the benefit of “Factor=Factor * 1” is

Assuming the “F” exists… and in fact is not nil (a check you should add by the way)…not sure where the Error is coming from

Tell the IDE to break on all errors, and let us know what line it blows on.

What version of OSX does it work on? and what version does it not?
/LIBRARY is NIL on WIndows… so this won’t work there… (yes I know you said OSX… just telling you incase you want to compile for both)
and I’m not so sure that that is not a restricted folder in later versions of OSX (I never can keep track)

Hi Dave,

          I still use (. AbsolutePath) because I use RS 2011 r4.3  for my projects, this because I'm not getting stability with COCOA Mouse Events and Glogal Floating Window (problems already reported in the feedback case 29734). 
          I use System Mac OS 10.9 and gives error on some machines and not others gives even with the same system, only to find no error gave MacMini Core i5 4GB Ram and Core 2 Duo 2.4 4GB Ram , but gave 2 iMacs all with Core2duo 2.0 3 GB RAM and System 10.9 .

[quote=68895:@Paulo Vargas]f=GetFolderItem(SpecialFolder.library.child(“Imageok”).child(“Temp.jpg”).AbsolutePath)
[/quote]
This line just needs to read

 f=SpecialFolder.library.child("Imageok").child("Temp.jpg")

BUT if the directory Imageok does not exist you can get a nil object exception
You should do something more like

f=SpecialFolder.library
if f is nil then
  // we have a big problem as the special folder. library doesn't exist
  return
end if
f = f.child("Imageok")
if f is nil then
  // we have a big problem as the special folder. library exists but somehow we can't get a child in it
  return
end if
if f.exists = false then // the ImageOK dir does not exist
  // decide what you SHOULD do 
  // maybe create the directory ?

  return // ...the image won't be in here for certain

else
  f=f.child("Temp.jpg")
  if f is nil then
    // we have a big problem as the specialfolder. library.ImageOK exists but somehow we can't get a child in it
    return
  end if 
  if f.exists = false then 
    // the temp.jpg does not exist
    // so yo ucant do anything with the picture - its not there :P
  else
    // you found the picture !
    // do whatever it is you need to with the picture
  end if
end if

Hi Norman,
I understand what you put but the fact is that this folder exists inside the machines and is already used for other functions in my project, even to save other images that are required for various functions but only this new mechanism is giving error and only on some machines what makes me think that the code is wrong.
The only thing that occurred to me is that the image is generated first step before the code was activated and you may save the image were slow to be done and with that being the object not found (Nil) but even put a timer to delay this process but still on some machines still gives the error, but what bothers me is wed on multiple machines never gives an error and gives other.

Paulo,
Since you never know what strange things a end user may have going on with their system, it’s a good idea to code defensively. Your code has neither error checking nor error reporting features - it’d be a good idea to add those.

Also, using AbsolutePath to re-create a folderItem is dangerous : i think it may fail if a user has two or more volumes with the same exact name.

Other things to watch out for:

  • users who are running OS X on a HFS Case-Sensitive filesystem
  • users who are running under FileValut 1 (which treats the Home directory as a disk image which has weird side effects)
  • users who are running in non-administrator accounts

etc.

Hi Michael,

          Yes it is true, several things can go wrong even! 
          I'll try to rewrite it and see what happens, do not know how I'll do but I am trying, thanks for now.