Home > Software engineering >  System.ArgumentNullException: 'Value cannot be null. Parameter name: path' hackerrank solu
System.ArgumentNullException: 'Value cannot be null. Parameter name: path' hackerrank solu

Time:08-15

Where exactly is the problem? "System.ArgumentNullException: 'Value cannot be null. Parameter name: path'" why could this be? Is there anyone who can help? Could the question have something to do with "Textwriter"?

here is my code

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Result
{

/*
 * Complete the 'aVeryBigSum' function below.
 *
 * The function is expected to return a LONG_INTEGER.
 * The function accepts LONG_INTEGER_ARRAY ar as parameter.
 */

public static long aVeryBigSum(List<long> ar)
{
        long sum=0;
        for(int i=0; i<= ar.Count;i  ) {
            sum  = ar[i];
        }
        
        return sum;
}

}

class Solution
{
public static void Main(string[] args)
{
    TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

    int arCount = Convert.ToInt32(Console.ReadLine().Trim());

    List<long> ar = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arTemp => Convert.ToInt64(arTemp)).ToList();

    long result = Result.aVeryBigSum(ar);

    textWriter.WriteLine(result);

    textWriter.Flush();
    textWriter.Close();
}
}

CodePudding user response:

I just ran your solution directly on Hackerrank and it does not throw that exception, so I would assume that you are running it locally.

When you run those solutions locally you need to be careful with the Environment Variables.

In this case the program expects an Environment Variable called OUTPUT_PATH which you probably did not set on your machine, but it is set on Hackerrank.

According to Microsoft, Environment.GetEnvironmentVariable returns:

The value of the environment variable specified by variable, or null if the environment variable is not found.

CodePudding user response:

ı solved the problem. change the function like this:

public static long aVeryBigSum(List<long> ar)
{
        long sum=0;
        foreach(long item in ar) {
            sum = sum  item;
        }
        
        return sum;
}
  •  Tags:  
  • c#
  • Related