0

I am trying to add a character to a char array but I receive a NullReferenceException when doing so. My code is as follows:

string[] triples = new string[10];
for(int i = 0; i < triples.Length; i += 3) {
  triples[i] = bits.Substring(i, 3);
}

char[] corrected = new char[triples.Length];
for(int i = 0; i < triples.Length; i++) {
  int zero = 0;
  int one = 0;
  
  foreach(char j in triples[i]) {
    if(j == '0') {
      zero += 1;
    } else {
      one += 1;
    }
  }
  
  if(zero > one) {
    corrected[i] = '0';
  } else {
    corrected[i] = '1';
  }
}

The issue arises on the corrected[i] = '0'; lines.

bits is a String

I have looked at this question, especially the section on arrays but I cannot find a solution

Orace
  • 7,822
  • 30
  • 45
T. Green
  • 323
  • 10
  • 20
  • 1
    Have you tried debugging your code? Attach the debugger, step through the code as it executes, and inspect the values being assigned? There are various screens to inspect values (locals, watches, quick watch to name a few). It should become clear what is null and why that triggers an NRE. That is what the answer to the [question](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) tries to illustrate, how to solve the NRE on your own. – Igor Jun 29 '21 at 17:31
  • 2
    In addition to the previous comment, please note that it's a violation of the community rules to [repost identical questions](https://stackoverflow.com/questions/68182989/nullreferenceexception-when-adding-to-char-array-c-sharp). As was already explained to you, the code you posted could not possibly throw the exception you claim it does. And you had already been provided the correct duplicate post to allow you to debug and solve the problem yourself. – Peter Duniho Jun 29 '21 at 17:37
  • `The issue arises on the corrected[i] = '0'; lines.` ← Do make sure that 1) you are running your code in "Debug" configuration mode and 2) that your code and running application match if you happen to de attaching your IDE to a already running instance. Either of those things could point you to the wrong line. If you are using a stack trace you need to realize that a stack trace line number might point you to the wrong line of code if the code was built in Release mode. The stack trace itself though *could* point you to the correct spot in the code. – Igor Jun 29 '21 at 17:40
  • 1
    `I have no debugging tools to use` ← It seems that it should not take too much effort to take the code you have posted in your question and create an [mcve] and run it locally using the IDE of your choice (visual studio for example) or even use an online tool like https://dotnetfiddle.net/. – Igor Jun 29 '21 at 17:43
  • 1
    @T.Green: _"The question is not identical"_ -- by the standards of this site, it absolutely is identical. Every question that asks for help debugging `NullReferenceException` is the same: the author of the question should be using a debugger to find and fix their own problem, and the duplicate question explains exactly how to do that. – Peter Duniho Jun 29 '21 at 17:48
  • 1
    "Also, the code does throw the exception I claimed it does" - I'd be absolutely astonished if it throws the exception at the line you said it did. As per Joel's answer, I'd expect it to be on the `foreach(char j in triples[i])` line. – Jon Skeet Jun 29 '21 at 18:10
  • @tgreen: your question is pretty much exactly identical (from memory). The difference is that you provided the declaration and initialization of `triples` (which you may remember was my suggestion). The accepted answer shows that that it is where your issue is, not in the code you insisted was the problem. What you should have done was edit your existing question, adding the requested code, not deleting and reposting _an identical question_. You also _really_ need to learn how to use a debugger; stop what you are doing and focus on that; it's more important than learning C# syntax for you – Flydog57 Jun 29 '21 at 18:29

2 Answers2

0

This first section might not be doing what you intend:

string[] triples = new string[10];
for(int i = 0; i < triples.Length; i += 3) {
  triples[i] = bits.Substring(i, 3);
}

After it runs, the triples array looks like this:

0 : bits 0 - 2
1 : null
2 : null
3 : bits 3 - 5
4 : null
5 : null
6 : bits 6 - 8
7 : null
8 : null
9 : bits 9 - 11

Later on, the code looks at every position in triples:

for(int i = 0; i < triples.Length; i++)

So when i increments to 1, and we come to this code:

foreach(char j in triples[i]) {

there is no value for that particular triples[i].

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • From the question: _"The issue arises on the `corrected[i] = '0';` lines"_. You have failed to explain why the exception occurs where the author of the question says it does. This does not answer the question that was asked. – Peter Duniho Jun 29 '21 at 17:38
  • @PeterDuniho the `corrected` array **IS** initialized, the `corrected[i]` elements are value types, and the right hand value is a constant. It's not possible to get that NullReferenceException on those lines. He's misreading something in his IDE or logs. Knowing that, I don't need to explain that part of his issue to tell him where the problem is in his code. – Joel Coehoorn Jun 29 '21 at 17:41
  • _"It's not possible to get that NullReferenceException on those lines"_ -- true, and that was explained to him [the first time they posted this question](https://stackoverflow.com/questions/68182989/nullreferenceexception-when-adding-to-char-array-c-sharp), before they deleted it and posted it again. But you still failed to address the question that was asked. – Peter Duniho Jun 29 '21 at 17:47
0

Here an analysis of your code.

// Set triples[i] for i = 0, 3, 6, 9 ...
for(int i = 0; i < triples.Length; i += 3) {
  triples[i] = bits.Substring(i, 3);
}


for(int i = 0; i < triples.Length; i++) {
  // Access triples[i] for i = 0, 1, 2, 3, 4 ...
  // So for i = 1, triples[i] is null and you get an exception
  foreach(char j in triples[i]) {
  }
}
Orace
  • 7,822
  • 30
  • 45
  • From the question: _"The issue arises on the `corrected[i] = '0';` lines"_. You have failed to explain why the exception occurs where the author of the question says it does. This does not answer the question that was asked. – Peter Duniho Jun 29 '21 at 17:38