Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to deal with this exception 1

Status
Not open for further replies.

purcion

Programmer
Feb 14, 2005
32
AU
I have created some code that searches the system for files
it then populates a Listview with the details the columns being
caption = path
subitem = filename
subitem = datemodified
the idea being that when the user clicks on the 'path'
column it shells explorer at the folder so you can see the file . This works fine when clicking on the path column, the problem is when the user clicks on a subitem/column like 'filename' it raises an exception it also raises an exception when the 'path' column is empty . Below is the problem code . I would really appreciate if someone could show me how to solve this problem.
thanks
CODE:
{=======================================================)
procedure TForm1.ListView1Click(Sender: TObject);
var
ListItem: TListItem;
addr:string;
begin
ListItem := Listview1.Items[
Listview1.ItemIndex] ;
edit4.Text := pchar(ListItem.caption);
addr:=edit4.Text;
ShellExecute(Handle, 'open', 'explorer.exe',Pchar(addr), 0, SW_SHOW);
end;
{=========================================================}

Thanks again
 
Well, I would do it like this:

Code:
procedure TForm1.ListView1Click(Sender: TObject);
 var
  ListItem: TListItem;
  addr:string;
begin
ListItem := Listview1.Items[
    Listview1.ItemIndex] ;
    edit4.Text := pchar(ListItem.caption);
    addr:=edit4.Text;
if ExtractFileExt(Addr) <> '' then
    begin
    if FileExist(addr) then
    ShellExecute(Handle,'open',PChar(Addr),nil,nil,SW_SHOWNORMAL);
    end
    else
    begin
    if DirectoryExist(addr) then
    ShellExecute(Handle,'open',PChar(Addr),nil,nil,SW_SHOWNORMAL);
    end;
end;

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
Thanks for that, but with the same senario it still throws exactly the same exception 'EAccessViolation' The debugger lands at this line when throwing the exception

addr:=pchar(ListItem.caption);




{======================================================}

procedure TForm1.ListView1Click(Sender: TObject);
var
ListItem: TListItem;
addr:string;
begin
ListItem := Listview1.Items[
Listview1.ItemIndex] ;
addr:=pchar(ListItem.caption);
//ShellExecute(Handle, 'open', 'explorer.exe',Pchar(addr), 0, SW_SHOW);
if ExtractFileExt(Addr) <> '' then
begin
if FileExists(addr) then
ShellExecute(Handle,'open',PChar(Addr),nil,nil,SW_SHOWNORMAL);
end
else
begin
if DirectoryExists(addr) then
ShellExecute(Handle,'open',PChar(Addr),nil,nil,SW_SHOWNORMAL);
end;
end;
{=======================================================}

Thanks
 
Oh, my bad, I didnt even see that.
Change this:

Code:
 var
 ListItem: TListItem;
 addr:string;
begin
ListItem := Listview1.Items[
    Listview1.ItemIndex] ;
    addr:=pchar(ListItem.caption);

To this:
Code:
var addr: string;
begin
addr := Listview1.Items[Listview1.ItemIndex];

And you should be all peachy :)

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
If you load 'ListItem' with a none initalised value (nil)
then you will get this exception.
Are you sure that your listview actually contains a value at the point that you load 'ListItem'.



Steve
Time for a new sig a bit of DNA I think will scan books and get back to you. It's taking a while isn't it!
 
I'm a dummy ... I meant to say:

Code:
addr := Listview1.Items[Listview1.ItemIndex].Caption;

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
thanks for your effort but it is still throwing the same exception
in the listveiw
there are three columns with files listed like so

path file modified
--------------------------------------------
c:windows a.txt 03/03/03 9:20 am

clicking on the [path] items will achieve sucess ie
opening explorer at the path address, if a user clicks
on an item in any other column
it throws an exception now it seems to me the reason being
that explorer cant read a path as a.txt it wont work (Im only guessing this)
Im stunned as to how I can well basically disable the list clicking in all coloumns except the path as that is the only one i want explorer to recieve. yes sggaunt it will throw the same exception if the list is empty but im thinking a user is less likley to click on an empty list

thanks for your help with this hopefully someone will know how to overcome this problem.
when the exception is created the debugger lands on this line

addr := Listview1.Items[Listview1.ItemIndex].Caption;

Updated code :
{======================================================}

procedure TForm1.ListView1Click(Sender: TObject);
var

ListItem: TListItem;
addr:string;

begin
//ListItem := Listview1.Items[
//Listview1.ItemIndex] ;
addr := Listview1.Items[Listview1.ItemIndex].Caption;
//addr:=pchar(ListItem.caption);
//ShellExecute(Handle, 'open', 'explorer.exe',Pchar(addr), 0, SW_SHOW);
if ExtractFileExt(Addr) <> '' then
begin
if FileExists(addr) then
ShellExecute(Handle,'open',PChar(Addr),nil,nil,SW_SHOWNORMAL);
end
else
begin
if DirectoryExists(addr) then
ShellExecute(Handle,'open',PChar(Addr),nil,nil,SW_SHOWNORMAL);

end;
end;

{======================================================}
 
Are the items in the other cols sub items? (they are normally), going back to my original suggestion you may be loading 'nil' as you dont access sub items.



Steve
Time for a new sig a bit of DNA I think will scan books and get back to you. It's taking a while isn't it!
 
take into account that Listview1.ItemIndex could be -1 so check this first before doing "addr := Listview1.Items[Listview1.ItemIndex].Caption;"

--------------------------------------
What You See Is What You Get
 
sggaunt I think your right I thought about what you said and that makes alot of sense , there is no value being loaded from the subitems you can see it in the code its only loading from the caption , I need to really learn more about the listview, it will come down to this point for sure.
I guess i need to try to load the subitems as well, but just not send them to explorer something along those lines . It would be good to have some code that overides the exception if nil value is loaded that would take care of a user accidently clicking on a empty listview as well ...I will play / experiment with loading the subitems see what i can get .
thanks guys.
 
There must be something Im missing or a bug in the Listview
control because if anyone can be bothered cutting an pasting the code below into their compiler and telling me why you get an exception when clicking on any other column
apart from the caption . That is like a gui type bug because your always going to get someone that clicks where there not s'posed to click surely there would have away around such an obvious error why make it possible if its going to throw an access violation. This is getting frustrating now.
Please Try it tell me and why this happens..
Thanks
{=======================================================}

{uses shellapi;}

procedure TForm1.FormCreate(Sender: TObject);
var
ListItem: TListItem;

begin
ListItem := ListView1.Items.Add;
ListItem.Caption :=('c:\windows');
ListItem.SubItems.Add('winhelp.exe');
ListItem.SubItems.Add('256048');
end;

procedure TForm1.ListView1Click(Sender: TObject);
var
ListItem: TListItem;
Laddr:string;
Lfile:string;
Lsize :string;
begin
Lfile:=ListView1.Selected.SubItems[0];
edit1.text := Lfile;
Lsize:=ListView1.Selected.SubItems[1];
edit2.text := Lsize;
Laddr := Listview1.Items[Listview1.ItemIndex].Caption;
ShellExecute(Handle, 'open', 'explorer.exe',Pchar(Laddr), 0, SW_SHOW);

end;
{========================================================}
 
how simple was it to fix


if Listview1.ItemIndex = -1 then Exit;
{========================================}
procedure TForm1.ListView1Click(Sender: TObject);
var

ListItem: TListItem;
addr:string;

begin

if Listview1.ItemIndex = -1 then Exit;
addr := Listview1.Items[Listview1.ItemIndex].Caption;
if ExtractFileExt(Addr) <> '' then
begin
if FileExists(Addr) then
ShellExecute(Handle,'open',PChar(Addr),nil,nil,SW_SHOWNORMAL);
end
else
begin
if DirectoryExists(Addr) then
ShellExecute(Handle,'open',PChar(Addr),nil,nil,SW_SHOWNORMAL);

end;
end;
{====================================================}

Thanks to everyone
 
purcion
You can test for 'nil' in the same way as you can test for any value.
also look at the try - except structure this can help you trap and handle exceptions i.e put up your own message or even do nothing at all(not recomended)

e.g this will trap the item index error, it wont stop 'nil' being loaded.
Note The appliction will run ok externaly but the debugger may still stop on the exception, this will depend on how it is set up.

try
addr := Listview1.Items[Listview1.ItemIndex].Caption;
except
showmessage('Your own message here')
end;

and you can do this
if addr = nil then
showmessage('no data');


Steve
Time for a new sig a bit of DNA I think will scan books and get back to you. It's taking a while isn't it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top