Lazarus

Programming => General => Topic started by: nikel on September 18, 2020, 10:58:17 am

Title: Check against null value
Post by: nikel on September 18, 2020, 10:58:17 am
I want the check objects for null value. Here's what I tried:

Code: Pascal  [Select][+][-]
  1. if (IDispatch(ListOfFolder[RandomFileNum]) = nil) then
  2. begin
  3.    ShowException(Exception.Create('File Error: Please check if file is accessible'));
  4.    Terminate;
  5. end;

This gives me index error doesn't show exception. How can I check for null value?
Title: Re: Check against null value
Post by: PascalDragon on September 18, 2020, 02:26:56 pm
You need to check whether RandomFileNum is inside the range of your ListOfFolder before accessing it.

Also you should not use Exception.Create to pass it along to ShowException, because the exception object will not be freed in that case. Better use MessageDlg or ShowMessage with only the string or use raise Exception.Create('...'); instead of ShowException(...) (you don't need the Terminate then either).
Title: Re: Check against null value
Post by: nikel on September 18, 2020, 08:06:59 pm
Thanks for the hint.
TinyPortal © 2005-2018