Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK .NET / Null Reference Exception in Arrays

Author
Message
Seppuku Arts
Moderator
17
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 17th Oct 2012 00:59
Maybe I'm missing something, but I've never fully understood exactly why I get these when I'm coding and usually assume I've got them covered, but it seems I'm struggling when I've got a class within a class and using arrays.

I normally manage to avoid these exceptions when I use something like this:

public Menu[] item = new Menu[4];

But stick that inside of another class (example with other declarations/method cut out):


From here, I declare:

public GameMenu StartMenu = new GameMenu();

So, as I've got 5 instances here, I should be able to in theory declare them from within a method inside of the same class as the above line:



How, I end up with the Null Reference Exception. I suspect that the null references are the strings themselves, but I can't use:
StartMenu.item[0].name = new string; because it won't let me.

I'm wondering what exactly I'm missing here. I don't fully understand why these errors occur anyway. The C# documentation doesn't exactly make it clear to me.

Diggsey
16
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 17th Oct 2012 01:09
First of all, when you run this from the IDE and it throws an exception, it should highlight the exact part of the line which causes the exception and give some fairly detailed information. You should also be able to put a breakpoint on that line and then examine the variables to see which of them is actually null.

However, in this case I am surprised it generated a null reference exception at all - it should instead be generating an array out of bounds exception. You are creating an array with four elements and attempting to put five things in it. This is obviously not going to end well

There is also no inherent problem with things being null - you can assign null to things and pass it around perfectly fine. The only problem is when you try to dereference that value, for example by using the '.' operator on it.

[b]
Seppuku Arts
Moderator
17
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 17th Oct 2012 11:30 Edited at: 17th Oct 2012 12:10
It occurs even when I've got the right array size. I am using VC# version 2008.

It's pointing to the line:

StartMenu.item[0].name = "New Game";

I comment that out and it'll go to the next array item.

Having a look at some of the debug info it seems that it's not the strings that are null but all instances of the 'Menu' array.

I tried adding this before I set the strings:
StartMenu.item = new GameMenu.Menu[5];

But I still get the same error, I would have thought it would have covered it. However, it seems setting these individually solved the problem:

StartMenu.item[0] = new GameMenu.Menu();
StartMenu.item[1] = new GameMenu.Menu();
StartMenu.item[2] = new GameMenu.Menu();
StartMenu.item[3] = new GameMenu.Menu();
StartMenu.item[4] = new GameMenu.Menu();

I suppose a 'for' statement will cover this array. But I wonder why I have to do it individually, shouldn't StartMenu.item = new GameMenu.Menu[5] create each instance rather than give them null?

Diggsey
16
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 17th Oct 2012 22:41
Ah, I can't believe I missed that. Yes, classes are reference types and structures are value types. If you had made an array of a structure you would therefore not need to initialize each element, but because it's an array of references, each one will be initialized to null.

One reason for this is that you potentially need to provide arguments to the constructor of a class (structures have to have an implicit default constructor which zero-initializes all their members) and you can't do this without initializing each element separately. Another reason is that if arrays automatically constructed their elements, there would be no way to have an array of nulls.

[b]
Seppuku Arts
Moderator
17
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 17th Oct 2012 23:33
Ah I was not aware of that, but when you think about it makes sense. And it's nothing difficult to accommodate for.


Cheers.

Login to post a reply

Server time is: 2022-08-07 16:32:57
Your offset time is: 2022-08-07 16:32:57