0

I am using the same code in my previous post but now I am trying to debug errors like

System.ArgumentNullException: 'Value cannot be null. (Parameter 'values')'

when I do

static void Main(string[] arg){
     int[] numbers = new int[] { 2, 4 };
     Console.WriteLine(String.Join(",", HowSum(7, numbers)));

}

How can I fix this when HowSum() returns a NULL?

Here is the original post for reference:

class HowSumSlow {

    static int[] HowSum(int targetSum, int[] numbers)
    {
        int[] empty = new int[] { };
        if (targetSum == 0) return empty;
        if (targetSum < 0) return null;

        
        
        foreach( var num in numbers){
            var remainder = targetSum - num;
            int[] remainderResult = HowSum(remainder, numbers);

            if (remainderResult != null){
                return remainderResult.Append(num).ToArray();
            }
        }
        return null;
    }
    static void Main(string[] arg) {
        int[] numbers = new int[] { 2, 3, 5 };
        Console.WriteLine(String.Join(",", HowSum(8, numbers)));
    }
}
5
  • 2
    Call HowSum first then check the return value and only if there is something returned try to format and display it. Commented Mar 2, 2022 at 15:15
  • 1
    int[] result = HowSum(8, numbers); Console.WriteLine(result is null ? "null" : String.Join(",", result)); Commented Mar 2, 2022 at 15:16
  • What do you expect to see as a result for HowSum(7, 2, 3, 5)? Do you expect {2, 2, 2}, with a remainder, or {2, 2, 3}? Commented Mar 2, 2022 at 15:49
  • @JoelCoehoorn Ah, I haven't implemented the part where there are different possibilities of the sum yet. At the moment with my current code, HowSum(7, [2,3,5]) would only show {2,2,3}, instead of both {2,2,3} and {2,5} Commented Mar 2, 2022 at 16:00
  • 1
    Okay. Also, I misunderstood what it was doing at first, and I see now {2, 2, 3} would have been correct from my options anyway. Commented Mar 2, 2022 at 16:02

4 Answers 4

2

How can I fix this when HowSum() returns a NULL?

You can use ?? to specify a fallback for the null array:

Console.WriteLine(String.Join(",", HowSum(7, numbers) ?? Array.Empty<int>()));

Now you are passing an empty int-array to String.Join if HowSum returns null.

Sign up to request clarification or add additional context in comments.

1 Comment

It seems to give me Operator '??' cannot be applied to operands of type 'int[]' and 'method group' error
1

just do a regular null check to find if the function returns null or not

if (HowSum(8 , numbers) != null) {
        Console.WriteLine(String.Join(",", HowSum(8, numbers)));
    } else {
        Console.WriteLine("ITS NULLLLL");
    }

Hope it helped. :)

3 Comments

Don't call the method twice just for checking. Use the returned value.
we can store it as a local variable and then check rite?
that is correct yes
1

In my previous answer (deleted since), I missed the recursion. So, you need the null return for the stop criterion.

Therefore, a negative targetSum value is a valid input while recursing but not as a start value.

So, what you could do, is to give it a "starter method" - like this:

// Your "HowSum" Method stays untouched!

static int[] StartHowSum(int targetSum, int[] numbers)
{
    if (targetSum < 0) 
    {
        throw new ArgumentOutOfRangeException (
            nameof(targetSum), targetSum,
            "Argument must be greater than or equal to 0."
            );
    }
    if (targetSum == 0) return Array.Empty<int>();
    // maybe also sanity-check `numbers`?

    int[] result = HowSum(targetSum, numbers);
    // Now that we checked input, is it possible to still get null OUTput?

    return result ?? Array.Empty<int>();
}

static void Main(string[] arg) {
        int[] numbers = new int[] { 2, 3, 5 };
        Console.WriteLine(String.Join(",", StartHowSum(8, numbers)));
    }

4 Comments

Ah, yes. I felt like your previous answer, though excellent in debugging the code, didn't perfectly suit the recursion part. I tried implementing everyone's comments and simply wrote int[] result = HowSum(7, numbers3); Console.WriteLine(result == null ? "null" : String.Join(",", result));. Would that be good enough?
"Would that be good enough?" Whatever works for you! If you (or whoever gave you this task) are satisfied with your approach, then it is good enough most of the time. The best learning I had from stuff that I once found "good enough" and later not anymore so much :D But this looks like some practice shots. So we don't need to overthink stuff, neither.
Yeah, as you can tell I am still new to codingXD, so I'm trying to make it as simple as possible to later dissect everything and understand it. But thanks for your input. If you don't mind, please don't delete this post so that I can refer to it for later debugging uses. Thanks.
Sure. I only deleted the other answer, because it was - well - wrong :(
1

After taking into account what everyone said, I found that the simplest way was to just store the result and use a ?-operator. (Thank you everyone. I wanted to write that in each and every comment, but apparently I'm supposed to refrain from that.)

Here's the final code.

static int[] HowSum(int targetSum, int[] numbers)
    {
        int[] empty = new int[0];
        if (targetSum == 0) return Array.Empty<int>();
        if (targetSum < 0) return null;

        foreach (var num in numbers)
        {
            var remainder = targetSum - num;
            int[] remainderResult = HowSum(remainder, numbers);

            if (remainderResult != null){
                return remainderResult.Append(num).ToArray();
            }
        }

        return null;
    }
    static void Main(string[] arg)
    {
        int[] numbers = new int[] { 2, 4 };

        int[] result = HowSum(7, numbers);
        Console.WriteLine(result == null ? "null" : String.Join(",", result));

    }
}

Comments

Your Answer

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