0

I am using regex code to find contact match in my windows phone application and it works good but gives an error sometimes "Values cannot be null".

try {
    var l = new List<string>();
    foreach (var item in listobj)
    l.Add(item.FirstName);

    var digitMap = new Dictionary<int, string>() {
        { 1, "" },
        { 2, "[abcABC]" },
        { 3, "[defDEF]" },
        { 4, "[ghiGHI]" },
        { 5, "[jklJKL]" },
        { 6, "[mnoMNO]" },
        { 7, "[pqrsPQRS]" },
        { 8, "[tuvTUV]" },
        { 9, "[wxyzWXYZ]" },
        { 0, "" },
    };

    var enteredDigits = str;
    var charsAsInts = enteredDigits.ToCharArray().Select(x => int.Parse(x.ToString()));
    var regexBuilder = new StringBuilder();
    foreach (var val in charsAsInts)
        regexBuilder.Append(digitMap[val]);

    var pattern = regexBuilder.ToString();
    //append a ".*" to the end of the regex to make it "StartsWith", beginning for "EndsWith", or both for "Contains";
    pattern = ".*" + pattern + ".*";

    var matchingNames = l.Where(x => Regex.IsMatch(x, pattern));
    SearchListbox.ItemsSource = listobj.FindAll(x=>x.PhoneNumbers.Contains(str) | x.FirstName.Contains(matchingNames.FirstOrDefault()));

} catch (Exception e) {
    MessageBox.Show(e.Message);
}

I think when no contacts match it throws an error, also it doesn't show all the matches until, whole name matches.

What's wrong? Thank you.

2
  • Can you tell me first please what are you actualy trying to do? This looks very convoluted to me for something that should be simple Commented Oct 9, 2014 at 9:40
  • Which line throws the exception? Commented Oct 9, 2014 at 9:42

1 Answer 1

1

When there is no match, matchingNames is null then matchingNames.FirstOrDefault() is also null

So here you'r doing String.Contains(null) => Exception

you see ? something like that :

if (matchingNames == null)    
{    
    //Search just in phoneNumbers    
    SearchListbox.ItemsSource = listobj.FindAll(x=>x.PhoneNumbers.Contains(str));    
}    
else    
{    
    //Search both    
    SearchListbox.ItemsSource = listobj.FindAll(x=>x.PhoneNumbers.Contains(str) |  x.FirstName.Contains(matchingNames.FirstOrDefault()));    
}

For your second problem try to remove completely the matchingNames and l variables :

try {
    var digitMap = new Dictionary<int, string>() {
        { 1, "" },
        { 2, "[abcABC]" },
        { 3, "[defDEF]" },
        { 4, "[ghiGHI]" },
        { 5, "[jklJKL]" },
        { 6, "[mnoMNO]" },
        { 7, "[pqrsPQRS]" },
        { 8, "[tuvTUV]" },
        { 9, "[wxyzWXYZ]" },
        { 0, "" },
    };

    var enteredDigits = str;
    var charsAsInts = enteredDigits.ToCharArray().Select(x => int.Parse(x.ToString()));
    var regexBuilder = new StringBuilder();
    foreach (var val in charsAsInts)
        regexBuilder.Append(digitMap[val]);

    var pattern = regexBuilder.ToString();
    //append a ".*" to the end of the regex to make it "StartsWith", beginning for "EndsWith", or both for "Contains";
    pattern = ".*" + pattern + ".*";

    SearchListbox.ItemsSource = listobj.FindAll(x=>x.PhoneNumbers.Contains(str) | Regex.isMatch(x.FirstName, pattern));

} catch (Exception e) {
    MessageBox.Show(e.Message);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Please state what you suggest to solve that issue. This isn't an answer to the question yet.
@Benji_9989 That solved all my problems :) Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.