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?
 
RabmisConnect With a Mentor Commented:
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.