Originally I did not include the "IF( p <> nil)". That bit me when I changed the app to dynamically download an image over the net. Of course networks are not reliable. When network failure occurred my image was not loaded. The side-effect of not loading is display of error messages irrelevant to my users. The solution was simple, if the image does not exist (nil) that means it never loaded, so don't bother trying painting a non-existent image.
// 'Paint' event handler on Canvas object. © 2010 Basil Bourque.
// This source code may be used freely by anyone taking full responsibility for doing so.
dim p as Picture = self.adImage
// Paint image only if it exists.
// Non-existent means the image failed to load.
if( p <> nil) then
// Add a border
g.forecolor = &cBBBBBB
g.penheight = 1
g.penwidth = 1
g.drawrect (0, 0, p.width, p.height)