-2

I am trying to convert integers (I got the integers from converting characters to their ascii codes) into an array of integers but when I run the program I get an error that says "Object Reference Not Set to an Instance of an Object" and it is referring to the last line in the code. What am I doing wrong here?

Thank you for any input, I am relatively new.

        foreach (char letter in arReverse)
        {
            int count = -1;
            count = count + 1;
            int num = (System.Convert.ToInt32(letter + 5));
            Console.Write(System.Convert.ToInt32(letter + 5));
            Console.Write(" ");
            arNum[count] = num;
        }
Dingo
  • 3
  • 1
  • 2
    Duplicate: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp May 14 '21 at 04:02
  • 2
    What are arReverse and arNum? Where are they declared and with what? If arNum is an [array](https://learn.microsoft.com/dotnet/csharp/programming-guide/arrays/single-dimensional-arrays) it must be [created](https://learn.microsoft.com/dotnet/csharp/programming-guide/arrays/). Example: `var arNum = int[arReverse.Length]`. Also, the loop is defective as count is misused: `int count = 0; foreach (char letter in arReverse) { ... arNum[count++] = num; }` ? –  May 14 '21 at 04:57

1 Answers1

0

I get an error that says "Object Reference Not Set to an Instance of an Object" and it is referring to the last line in the code. ... What am I doing wrong here?

arNum[count] = num; may not be initialized.

Make sure the array/list arNum is initialized. This is done with the new keyword such as

var arNum = new List<int>();
var arNum = new int[arReverse.Length];
DekuDesu
  • 2,224
  • 1
  • 5
  • 19