Repairing a Defect

The runtime tracing feature has uncovered what looks to be an unhandled case - that is, handling a phone number of 0 length. The code must be fixed.

To fix the defect:

  1. In the Runtime Trace viewer, left-click the green PhoneNumber constructor call made by the Test Case

Take a look at this PhoneNumber constructor (you may need to scroll down a bit in order to fully expose the function). In essence, a numberString object is being prepared to hold the phone number. What happens if the length of the phone number - the input to this constructor  - is 0? The numberString object is never created.

The problem is the last line of this constructor. The numberString object is assigned a final value. How can this be if the numberString object is never created when the length of the phone number is 0? You need to add an extra line of code to ensure that the last line of the constructor is only executed if the length of the phone number is greater than 0.

  1. Modify the source code of this PhoneNumber constructor as follows:

    if(length > 0)     

    numberString[length] = '\0';


In other words, add the if statement.

  1. Select the menu item File->Save

This should fix the problem. In the next topic, you will rerun your test to make sure the unexpected exception goes away.