0

I receive a null issue when debugging a c# array.

Visual studio debugging show it has a null something.

There is a class storage.

public class db
{
    public string uid;
}

It was created this way:

db[,,] tmpdb = new db[16, 16, 16];

When I try to use it:

db[x,y,z].uid = "anID";

What I get is: System.NullReferenceException: 'Object reference not set to an instance of an object.'

When I try:

string[,,] sdb = new string[16,16,16];

It actually work fine!

EDIT: what's the different and why is the first a null and this not a null? By logic, they are created the same way?

Does it not work the same? Should I use this instead?

List<List<List<db>>>
  • 4
    db[x,y,z] is null unless you have set it to something. If it is null, db[x,y,z].uid will raise a NullReferenceException. You need to do db[x,y,z] = new db() first. – Erlend D. Jan 09 '19 at 11:57
  • `new db[16, 16, 16];` des **not** create 4096 instances of your `db`-class, but just an array that **may** hold so many instances. So the objects don´t exist, accessing one at a specific index will throw that exception. – HimBromBeere Jan 09 '19 at 11:58
  • it is set to tmpdb? – user3014330 Jan 09 '19 at 11:59
  • You have to create the **instances** within the array, not just the array itself: `var arr = new db[] { new db { ... }, new db { ... }, new db { ... } ... }`. – HimBromBeere Jan 09 '19 at 12:01
  • With List<> I could use .add, how do I do it with this? – user3014330 Jan 09 '19 at 12:01
  • 1
    A simple example: var myArray = new db[5] creates an array of db-objects, but they are all null by default. That means that this array looks like this: [null, null, null, null, null], and NOT like this: [db, db, db, db, db]. You need to create instances before you can alter them. Using List<> would be exactly the same. – Erlend D. Jan 09 '19 at 12:03
  • I could loop the list and go, db.add(); But I still do not understand how the default use is not null and value could be put without worrying about it being null...? – user3014330 Jan 09 '19 at 12:06
  • Yes, but you could do the same in the array. for(var x=0;x<16;x++)for(y=0;y<16;y++)for(z=0;z<16;z++) tmpdb[x,y,z] = new db() is probably the code you're missing, and you need to do the equivalent of this even if you use List<>. The problem is that your array/list contains null values (or no values) when it is instantiated, which means that you need to create db objects before you can work with them, no matter what type of collection you are using. – Erlend D. Jan 09 '19 at 12:08
  • string[,,] sdb = new string[16,16,16]; are not null upon creation? – user3014330 Jan 09 '19 at 12:11
  • You need to go and read up on the difference between a value type and a reference type in C#. – DavidG Jan 09 '19 at 12:11
  • Basiccally, it create a link, but not the actual volume? Man, even other code was not so stupid... – user3014330 Jan 09 '19 at 12:12
  • https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c – DavidG Jan 09 '19 at 12:12
  • string[,,] sdb = new string[16,16,16]; are null on creation. Doing sdb[x,y,z] = "abc"; will assign a string to that array value so you will not get an exception. If you tried to get the length of string using sdb[x,y,z].Length before assigning the string, you would get the same exception. – PaulF Jan 09 '19 at 12:16
  • I tried this, the error is gone: tmpdb [x,y,z] = new db(); But now the for loop return a: System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'? NVM, I fix it... – user3014330 Jan 09 '19 at 12:16

0 Answers0