System.ArgumentNullException: 'Value cannot be null. Parameter name: path' hackerrank solution aVeryBigSum - c#

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();
}
}

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.

ı 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;
}

Related

error return keyword must not be followed by an object expression

The return(match) is giving me a headache! When I change it into Console.WriteLine(match) it gives me the return I expect but when I try to use the return(match) it gives me an error. I just don't know what to change here so any suggestions would be appreciated!
regards, James
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ReadTextFile
{
class Program
{
static void Main(string[] args)
{
List<string> fileLines = new List<string>();
using (var reader = new StreamReader("test.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
fileLines.Add(line);
string pattern = #"(\w+)#(\w+).([a-z]+)";
Match match = Regex.Match((line), pattern);
if (match.Success)
{
return(match);
}
}
}
Main returns a void if you want to return something else change the signature. Note that typically the return value of Main is called an error code where non-zero is considered an error.
If you create another function and put your logic into it you could then use return.
On the other hand if all you are looking to do is leave the while you instead need to make Match match; be after string line; and use break; to get out instead of return.
You're in a void method (void Main). void methods do not have a return value, so just return; by itself is all that's allowed.
BTW, return is not a function in C#, it's a statement, so there is no need for parenthesis.

CS1955 C# Non-invocable member 'Match.Date' cannot be used like a method. I'm getting an error like this,can someone help me please.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Matches
{
class Program
{
static void Main(string[] args)
{
MatchesContext db = new MatchesContext();
Match match = new Match();
match.M_ID = 4;
string sdate = "2015-03-10";
DateTime dt = Convert.ToDateTime(sdate);
match.Date(sdate);
match.Team1 = "Kolkata";
match.Team2 = "Mumbai";
match.Umpire = "Benson";
db.Matches.Add(match);
db.SaveChanges();
Console.WriteLine("object is created");
Console.ReadKey();
}
}
}
Non-invocable member 'Match.Date' cannot be used like a method.This is the error i'm getting.Can someone please help me
It seems Date is a property, not a method. If you want to assign a value to it, use
match.Date = dt;

Skype issue with multiple params

I am trying to make a C# Stress script but it keeps giving me the error: Error Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.
Code:
using System;
using System.Collections.Generic;
using System.Text;
using Skyper;
using SKYPE4COMLib;
using System.Net;
namespace Skyper.plugins
{
public static class Help
{
public static string Description
{
get
{
return "Stresser";
}
}
public static void Execute(string[] Params, int chat, string username)
{
Skyper.SendMessage(chat, Params[1] + "" + new WebClient().DownloadString("http://example.com/stresser/api.php?key=examplekey&host=" + Params[1]));"&port=&time=&method=";
}
}
}
How this script will work is users in Skype will type in !stress ip, port, time, method and then it will submit it to the API.
Following is a simple string, which is not assigned to any thing,
"&port=&time=&method=";
So may be you can use something like:
Skyper.SendMessage(chat, Params[1] + "" + new WebClient().DownloadString("http://example.com/stresser/api.php?key=examplekey&host=&port=&time=&method=" + Params[1])");
or change that string properly with double quotes and brackets end symbols

How to print out the result of a SWI-Prolog query from C# using Swi-cs-pl library

I'm trying to understand how the Swi-cs-pl library works by doing my own little program, but I cannot printout any result from a query based on the example from SWI-Prolog web.
I have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SbsSW.SwiPlCs;
namespace HolaMundo
{
class Program
{
static void Main(string[] args)
{
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" }; // suppressing informational and banner messages
PlEngine.Initialize(param);
PlQuery.PlCall("assert(father(hader, nevar))");
PlQuery.PlCall("assert(father(hader, sergio))");
PlQuery.PlCall("assert(brother(nevar, sergio):- father(hader, nevar), father(hader, sergio))");
//How do I write out in a Console the answer for a query like:
// brother(nevar, sergio)
PlEngine.PlCleanup();
}
}
}
}
As I said on my code, I just want to query something basic like: brother(nevar, sergio). and get any answer from Console like true or whatever.
Can anyone help me?

Issue trying to solve Project Euler #1

I'm having a problem trying to make a small app to solve Project Euler Problem #1.
Whenever I attempt to run this, it returns as 0, instead of 233168.
I'm not necessarily looking for an absolute answer, just some hints, I'm trying to learn.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x;
List<int> listOne = new List<int>();
for (x = 1; x < 1000; ++x)
{
if(x%3 == 0 || x%5 == 0)
{
listOne.Add(x);
}
Console.WriteLine(listOne.Sum());
Console.ReadLine();
}
}
}
}
In the interests of helping you learn, I'm not going to provide the exact answer.
Have a look at the scoping of your Console.WriteLine() statement. My guess is that it's not running when you think it should be.

Categories

Resources