- Home /
Array member treated as null even though it is not.
I have a Block[] Blocks
, and the class Block contains a string Name
.
I also have a Dictionary<string,int> Names
, in which the index of a block in the blocks array is stored with that block's name as the key.
in a loop meant to populate the dictionary using the block array: i get an error, saying that the field 'key' cannot be null
Here is a screenshot from Visual Studio:
Answer by xxmariofer · Jan 18, 2019 at 10:46 PM
can you Debug.Log i before you populate the disctionary? Also did you initialize the Dictionary somewhere else using new keyword?
there is a debug.log line, which fails to run, even when the debugger steps to it. Inspector says it is not null, but still fails in the loop.
This loop is taking place inside a constructor for a static class, by the way, in case that is important
How many times is entering the foreach before crashing? the first is assigned but maybe other blocks are null and isnt crashing in the first loop?
it crashes immediately, when i = 0, the first item in Blocks. Every block in the array has "name" defined, and i can see them through the debug tools.
Your answer
Follow this Question
Related Questions
Can´t instantiate objects in list correctly 1 Answer
Tagged objects not returning value 0 Answers
Multiple Cars not working 1 Answer
Can't find source of trigger Null Reference Exception 1 Answer
GetComponents array in C# error ? 4 Answers