Program crashing if tree is empty

My program crashes when my tree is empty. When i search for a value that is in the tree it finds and displays it fine. Yet if i enter a value that is not in the tree it will crash.

Also my tree gets overwritten after one sucessful search.

What am i doing wrong?

Prima12Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnjcesCommented:
If you can post some code of how you are searching, that could help out a bunch.

John
0
Prima12Author Commented:
Doh!

All the code that creates the tree and all that stuff is 100% fine and tested. The problems are within these two pieces of code.


function tuneSearching(var t:melodyTree; phraseSearch:string; element:integer):melodyTree;
begin
  if (element <= length(phraseSearch)) then begin
      if(upcase(phraseSearch[element]) ='D') then begin
        tuneSearching:= tuneSearching(t^.downBranch, phraseSearch, element+1);
      end else begin
 
      if(upcase(phraseSearch[element]) = 'U') then begin
        tuneSearching:= tuneSearching(t^.upBranch, phraseSearch, element+1);
      end else begin
 
      if(upcase(phraseSearch[element]) = 'S') then begin
        tuneSearching:= tuneSearching(t^.sameBranch, phraseSearch, element+1);
      end;
      end;
      end;
      end else begin
        tuneSearching:= t;
  end;
end;
 
procedure tuneSearch(var t:MelodyTree; phraseSearch:string);
begin
  phraseSearch:= trim(phraseSearch);  //removes extra whitespacing
  if (length(phraseSearch)>0) then //test the string is not empty
  begin
    t:= tuneSearching(t, phraseSearch, 1);
    writeln('A match was found: phrase ', t^.phrase, ' in the song ', t^.title);
  end else
    writeln('There was no input please try again');
end;

Open in new window

0
TheRealLokiSenior DeveloperCommented:

function tuneSearching(var t:melodyTree; phraseSearch:string; element:integer):melodyTree;
begin
  result := nil; // default
...
... etc...
 
procedure tuneSearch(var t:MelodyTree; phraseSearch:string);
begin
  phraseSearch:= trim(phraseSearch);  //removes extra whitespacing
  if (length(phraseSearch)>0) then //test the string is not empty
  begin
    t:= tuneSearching(t, phraseSearch, 1);
    if assigned(t) then
      writeln('A match was found: phrase ', t^.phrase, ' in the song ', t^.title)
    else
      writeln('A match was not found');
  end
  else
    writeln('There was no input please try again');
end;

Open in new window

0
RabmisCommented:
Access violation was fixed in this code
function tuneSearching(var t:melodyTree; phraseSearch:string; element:integer):melodyTree;
begin
  tuneSearching := nil;
  if not assigned(t) then // Check to nil refence
    Exit;
  if (element <= length(phraseSearch)) then begin
      if(upcase(phraseSearch[element]) ='D') then begin
        tuneSearching:= tuneSearching(t^.downBranch, phraseSearch, element+1);
      end else begin
 
      if(upcase(phraseSearch[element]) = 'U') then begin
        tuneSearching:= tuneSearching(t^.upBranch, phraseSearch, element+1);
      end else begin
 
      if(upcase(phraseSearch[element]) = 'S') then begin
        tuneSearching:= tuneSearching(t^.sameBranch, phraseSearch, element+1);
      end;
      end;
      end;
      end else begin
        tuneSearching:= t;
  end;
end;
 
procedure tuneSearch(var t:MelodyTree; phraseSearch:string);
begin
  phraseSearch:= trim(phraseSearch);  //removes extra whitespacing
  if (length(phraseSearch)>0) then //test the string is not empty
  begin
    t:= tuneSearching(t, phraseSearch, 1);
    if assigned(t) then
      writeln('A match was found: phrase ', t^.phrase, ' in the song ', t^.title)
    else
      writeln('A match was not found');
  end else
    writeln('There was no input please try again');
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TheRealLokiSenior DeveloperCommented:
I believe my solution stopped the crash and is still valid.
Just because you "dont need an answer to this question anymore" does not mean the answers are wrong...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.