-1

I've tried to save args[0] into string lemon, but whatever I tried it didn't work at all, it instead threw an IndexOutOfRange exception:

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at NHBloater.Program.Main(String[] args) in C:\Users...\Program.cs:line 12

I tried:

  • catching it
  • adding if (args.Length > 0)
  • searching on Google and Stack Overflow

Edit: I fixed it but don't know how, and the code that's worrying me is output += inputArray[i]

Here is the code with the first line being line 10 without the attempts to fix it: c#:

static void Main(string[] args)
{
    string lemon = args[0];
    string input = File.ReadAllText(lemon);
    string[] inputArray = input.Split();
    string output = "";
    for (int i = 0; i < input.Length; i++) 
        for (int j = 0; j < Convert.ToInt32(args[2]); j++) 
            output += inputArray[i];
    File.WriteAllText(args[1], output);
}

Imported libraries:

  • System
  • System.IO
  • System.Text

Arguments:

"h.txt"
"h2.txt"
"5"
Matto58
  • 27
  • 1
  • 6
  • Your code actually assumes `args` has no less than three arguments since it goes so far as to access `args[2]`, so you probably want a check for that at the beginning. – Jeroen Mostert Apr 14 '22 at 11:27
  • 2
    when you don't provide any command-line args, what do you expect `args` to contain? – MakePeaceGreatAgain Apr 14 '22 at 11:28
  • 1
    `i < input.Length` + `output += inputArray[i];` = boom – Cid Apr 14 '22 at 11:32
  • You say you provided the arguments but have you actually debugged your code and looked inside of the args array and seen whats in there? – Ralf Apr 14 '22 at 11:37
  • @Ralf that's a pretty good idea, I'm stupid that I didn't do that earlier. – Matto58 Apr 14 '22 at 11:39
  • outputs System.String[], i dunno why – Matto58 Apr 14 '22 at 11:42
  • That looks like the output of a call to args.ToString() and not what debugging the code should show you if you look at the args array in the debugger. – Ralf Apr 14 '22 at 11:44
  • Put a breakpoint (do you know how to do that?) at that line and see. – ispiro Apr 14 '22 at 11:44
  • Unrelated: `Convert.ToInt32(args[2])` Do not trust user input. – Fildor Apr 14 '22 at 11:44
  • Are you sure that you have an exception at lemon assignment? I copied your code and I see exception at `string input = File.ReadAllText(lemon);`, because I don't have such file. – Lukasz Szczygielek Apr 14 '22 at 11:46
  • @Ralf, i used Console.WriteLine(args); – Matto58 Apr 14 '22 at 11:46
  • 2
    That calls args.ToString that is not helpful. You should look up how debugging actually works. Its an extremely helpful tool to understand anything. And its actually easy todo if you have it done at least once ;) – Ralf Apr 14 '22 at 11:48
  • @ispiro, i added a break point and found it working, pretty strange – Matto58 Apr 14 '22 at 11:48
  • what does `args` contain when debugging? Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – MakePeaceGreatAgain Apr 14 '22 at 11:49
  • The link to the documentation with a quick start on debugging https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2022 – Ralf Apr 14 '22 at 11:51
  • @MakePeaceGreatAgain, it contains "h.txt","h2.txt","5" which is what I wanted – Matto58 Apr 14 '22 at 11:54
  • [Debug.Assert](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert?view=net-6.0) can also be really helpful, sometimes to quickly check if your assumptions are actually met. – Fildor Apr 14 '22 at 11:54
  • Can you comment out the complete for-loops and see if it still throws? – Fildor Apr 14 '22 at 11:57
  • 2
    The exception you posted says it occurred at line 12. Which is line 12? Lukasz's question was never answered. – gunr2171 Apr 14 '22 at 11:57
  • @gunr2171 Line 12 is `string lemon = args[0];`, but I fixed it without knowing how – Matto58 Apr 14 '22 at 12:05
  • You fixed it by modifying the code, changing the command line arguments, and/or changing the contents of the file you are reading. We don't know what any of those modifications are. Next time please post a [mre]. – gunr2171 Apr 14 '22 at 12:06
  • @gunr2171 I apologize for that, that might be the case. The thing that's worrying me is `output += inputArray[i];` – Matto58 Apr 14 '22 at 12:08
  • 1
    Why would that be worrying you? You just said that `string lemon = args[0];` - the first line of the program - is the line throwing the exception. _NO OTHER STATEMENTS MATTER_. If the first line of the program throws an exception, then the program will never execute the second line. – gunr2171 Apr 14 '22 at 12:10
  • Also, please do not edit your question with an answer. You can answer your own question using the answer box. – Lee Taylor Apr 14 '22 at 12:42

3 Answers3

2

While args won't be null (See the green Tip box in the link), it might be an Array of length 0.

So args[0] doesn't exist because it refers to the first item in the array, which doesn't have any items.

If you are really setting it in "command line arguments" in Visual Studio and are really using debug mode - see this answer. Basically - make sure it's all on "Any CPU".

EDIT

Change

string[] inputArray = input.Split();

to

string[] inputArray = input.ToCharArray();
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • The array has a length of 3. – Matto58 Apr 14 '22 at 11:36
  • I like providing the link to the documentation. The docs are pretty consice on the topic. But pointing out the null problematic while that isn't his problem (he got a IndexOutOfRangeException) is distraction from the things he should read in the docs. – Ralf Apr 14 '22 at 11:36
  • @Matto58 If you put a breakpoint on line 12 and then hover over `args`, I'm pretty sure it will tell you it's empty. (Though I don't know how you're testing it so I don't know why). – ispiro Apr 14 '22 at 11:38
  • @Matto58 How _are_ you passing those arguments to Main? – ispiro Apr 14 '22 at 11:39
  • @ispiro Project > Properties > Debug > Command line arguments which is h.txt h2.txt 5 – Matto58 Apr 14 '22 at 11:44
  • @Matto58 And how are you running it, by pressing F5 (or clicking Debug)? Or are you clicking the exe file (which won't work)? – ispiro Apr 14 '22 at 11:46
  • @Matto58 Also, is Visual Studio in `Debug`? – ispiro Apr 14 '22 at 11:46
  • @ispiro Using F5 and yes, it's in Debug. There's a file called h.txt inside the Debug folder so that should work, btw, the thing that now errors is actually `output += inputArray[i]` and it'S the same exception – Matto58 Apr 14 '22 at 11:51
  • @Matto58 BTW see my edit - the new (last) paragraph in my answer. – ispiro Apr 14 '22 at 11:52
  • @ispiro I indeed use Any CPU, pretty weird – Matto58 Apr 14 '22 at 11:55
  • @Matto58 The best thing for you to do now it to see [this youtube video](https://youtu.be/7ab4z9u7Q_I?t=77) I set it to the correct time in the video to show you how to put a breakpoint. Breakpoints can be immensely helpful. After setting a breakpoint and running, Visual Studio will stop at that line and then you can hover over variables to see their values. – ispiro Apr 14 '22 at 11:57
  • @ispiro I know how to do it - thank you, but the values are correct, but that's no longer the issue. – Matto58 Apr 14 '22 at 12:06
  • @Matto58 So now that the problem is in that other line - put a breakpoint on it and hover over `inputArray` and see if it has values. If it does, hover over them to get their value. Etc. – ispiro Apr 14 '22 at 12:08
  • @ispiro Thanks to you, I _almost_ solved it! The problem is now in `string[] inputArray = input.Split();`, where it returns {"Hello"}, which are contents of the file h.txt. I want it to output {"H","e","l","l","o"}. – Matto58 Apr 14 '22 at 12:13
  • @Matto58 See my edited answer. – ispiro Apr 14 '22 at 12:15
1

You are checking input.Length but you are accessing inputArray[i].

Fildor
  • 14,510
  • 4
  • 35
  • 67
InUser
  • 1,138
  • 15
  • 22
-3

How do you start Main function (program)? Do you pass arguments to Main function? If not, lenght of your args array is 0 (you don't have any fields in that array).

CROnoob
  • 67
  • 1
  • 1
  • 8