4

I have following code definig an array

 public class PalphabetsDic
 {
     public static string[] PAlphCodes = new string[3] {
         PAlphCodes[0] = "1593",
         PAlphCodes[1] = "1604",
         PAlphCodes[2] = "1740",
     };
 }

When I use this array

var text = PalphabetsDic.PAlphCodes[1]

Gives error:

The type initializer for 'Dota2RTL.PalphabetsDic' threw an exception. ---> System.NullReferenceException: Object reference not set to an instance of an object.

Please can someone help me on this?

Note that What is a NullReferenceException, and how do I fix it? covers arrays, but PAlphCodes = new string[3] should be setting it up to be not null.

Community
  • 1
  • 1
Ali Padida
  • 690
  • 7
  • 20
  • I initialized the array, that guy didn't. I got headache. It's been a day I'm searching about it. – Ali Padida Jan 23 '15 at 02:51
  • 1
    No, you didn't initialize the array, that's why you have a type initialization exception - the null reference exception you're showing is an inner exception of said type initialization exception. To be fair, I'm somewhat surprised your declaration of `PAlphCodes` even compiles. – Preston Guillot Jan 23 '15 at 02:55

3 Answers3

9

When initializing the way you are you don't need to index the values:

public static string[] PAlphCodes = new string[] {
            "1593",
            "1604",
            "1740",
        };
  • You can also use `string[] PAlphCodes = { "1593", "1604", "1740" }`. Both options are valid and commonplace. – debracey Jan 23 '15 at 02:52
  • True. I was trying to change his code as little as possible so he could see what wasn't needed. I should have included both forms though. – Kennedy Bushnell Jan 23 '15 at 02:55
  • Glad to help. Also, welcome to Stack Overflow. Please remember to upvote questions/answers that you find useful, and don't forget to accept the most helpful answer by clicking the check mark. – debracey Jan 23 '15 at 02:59
  • I still need 15 reputation. xD – Ali Padida Jan 23 '15 at 03:01
4

To expand upon what Kennedy answered -- you can also use

public static string[] PAlphCodes = { "1593", "1604", "1740" };

The reference manual has a listing of all the possible ways -- but the one Kennedy suggested -- and this method -- are probably the most common.

https://msdn.microsoft.com/en-us/library/aa287601(v=vs.71).aspx

debracey
  • 6,230
  • 1
  • 28
  • 56
1

Indeed you've used strnage syntax to initialize array as pointed out in other answers and something like static string[] PAlphCodes = new []{"1","2","3"}; would fix the problem.

On why this actually compiles (which is somewhat surprising for most people):

You can use static fields to initialize other static fields, but surprisingly you can also refer to static field inside initialization if the field itself. So there is no compile time error.

It fails at run-time first with NullReferenceException because initialization of the array is not completed by the time it is used for first time - so PAlphCodes is null while array is created. But since this is part of class level initialization (as it is static filed) this exception also stops class instance creation and you get "The type initializer ...." wrapping NullReferenceException .

Note that in most cases such construct would not even compile. I.e. using it in non-static field of local variable fails at compile time with

A field initializer cannot reference the non-static field, method, or property ...

public class PalphabetsDic
{
 public string[] PAlphCodes = new string[3] {
     PAlphCodes[0] = "1593", // error here and other lines
     PAlphCodes[1] = "1604",
     PAlphCodes[2] = "1740",
 };
}
Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159