NilObjectException after dropping onto a canvas?

Hi,
I have the following code in the canvas.open event, which SHOULD only allow image files to be dropped, BUT, if I drop a .txt file onto the canvas - I get a NilObjectException??

Could someone please advise me what I am doing wrong, or what I am missing?
(FileTypes2 contains .png, .jpg, and .psd).

Thank you.

[code] // ACCEPT PNG, JPG AND PSD FILES

me.AcceptFileDrop(FileTypes2.All)[/code]

Do you have some Try/Catch and/or Error Handling in your app?

What code do I need to add then, in order to catch this particular error?

Thanks.

Can you show us your canvas.open event?

A NOE always means that you are talking to something that isn’t there. So typically you would add a NIL check. Or you wrap your code in a try/catch block. For instance:

[code]Function Equal(extends firstArray() as String, secondArray() as String) As Boolean

'simple check for UBound equal or not

if firstArray = nil or secondArray = nil then Return False

try
if UBound(firstArray) <> UBound(secondArray) then Return false

for currentOb as Integer = 0 to UBound(firstArray)
  if firstArray(currentOb) <> secondArray(currentOb) then Return false
next

catch err as NilObjectException
Return false
end try

Return true
End Function[/code]

But you really should have generic error handling. All my methods end with

exception exc theException = new ErrorException(exc, currentMethodName)

where ErrorException is a custom error handler showing a nice window to the user, allows sending a mail etc.

[quote=117936:@Richard Summers]Hi,
I have the following code in the canvas.open event, which SHOULD only allow image files to be dropped, BUT, if I drop a .txt file onto the canvas - I get a NilObjectException??

Could someone please advise me what I am doing wrong, or what I am missing?
(FileTypes2 contains .png, .jpg, and .psd).

Thank you.

[code] // ACCEPT PNG, JPG AND PSD FILES

me.AcceptFileDrop(FileTypes2.All)[/code][/quote]

Cocoa seems to have trouble discriminating file types when it comes to pick them. That is the case in dialogs, would make sense that is what happens here. The NIL is probably happening in your DropObject code where you open the file as picture.

You want to add a check in your DropObject that it is indeed a file you want. Something like this should do :

Sub DropObject(obj As DragItem, action As Integer) if instr(obj.folderitem.name,".jpg")+instr(obj.folderitem.name,".jpeg")+ _ instr(obj.folderitem.name,".png")+instr(obj.folderitem.name,".psd") = 0 then return // Here you open your file and do what you got to do ... End Sub

If you still get a Nil exception, please post the code you have in DropObject.

Thanks everyone - especially Michel - your code worked perfectly.
Much appreciated.

Hi,

Michel told you the essential here.

But what you do not say in your question - and this is important - is: you’re running OS X…

Also, since this is specific to Cocoa, you can add your code in a #If TargetCocoa …/… #EndIf block. So, if in the future you decide to compile for Lnux / Windows, you do not execute that useless code (for these platforms).

Thanks Emile.