Array Problem: Object reference not set to an instance of an object.

Posted 10 January 2012 - 09:57 PM

I am getting the error:

A first chance exception of type 'System.NullReferenceException' occurred in BudgetSheet.exe
An unhandled exception of type 'System.NullReferenceException' occurred in BudgetSheet.exe
Additional information: Object reference not set to an instance of an object.

When I try to load items into my arrays. I have tried searching online and nothing so far has solved my problem. I just need these array to get loaded and then my code should all work properly.

Replies To: Array Problem: Object reference not set to an instance of an object.

Re: Array Problem: Object reference not set to an instance of an object.

Posted 10 January 2012 - 11:09 PM

TechnoBear, on 10 January 2012 - 10:57 PM, said:

I am getting the error:

A first chance exception of type 'System.NullReferenceException' occurred in BudgetSheet.exe
An unhandled exception of type 'System.NullReferenceException' occurred in BudgetSheet.exe
Additional information: Object reference not set to an instance of an object.

When I try to load items into my arrays. I have tried searching online and nothing so far has solved my problem. I just need these array to get loaded and then my code should all work properly.

I'm sure that this situation must have been brought up somewhere but I have as of yet been unable to find it.

Try using a list(of T) object. It's a lot easier to use. Your problem stems from your arrays like m_files. I'm not sure exactly why the error pops up but it does. Redim the array to a specific size, or try the list of object. Anytime I have a variable that is declared with no definite size I get this error.

Edit: Correction not every time. Just anytime you need it with something relatively complex hah.

Re: Array Problem: Object reference not set to an instance of an object.

Posted 10 January 2012 - 11:32 PM

When you have a list you have to use the item.add method of the object to add a string to it. You're getting the error in that line because m_files is still a dynamic size. Give it a definite size either dim it as m_files(25) or something along those lines. So figure out how big the array needs to be and declare it, you can even use redim and change the size later on.

Re: Array Problem: Object reference not set to an instance of an object.

Posted 10 January 2012 - 11:33 PM

For the love of pete.. first - instantiate your variables.. good lordy.. bad practice.

Second.. look at what your loop is doing (line 12).

You start at zero.. and when the for loop starts it increments before you use it to access the array item.. so you never fill position 0... and as it goes you'll loop one more time than there are positions in your array thus giving you the null ref error.

Example:
x = 0
y() = {a, b, c}

for each z in y
x+=1
print y(x)
next

x = 0

loop starts
x=1
prints b

second iteration
x = 2
prints c

third iteration
x = 3
error

So perhaps increment AFTERWARDs.. or be on the safe side and use a for loop that counts from zero to the length of diar1 - 1.

@trevster - did you really have to quote the post right above you? Way not necessary.

@trev- good catch.. I thought I saw the array was declared right.. guess not.

Re: Array Problem: Object reference not set to an instance of an object.

Posted 11 January 2012 - 09:21 AM

ok i have managed to solve Sub Load() using redim. However, a little later in the code I am trying to add elements to the array and if i redim here it clears all previously entered data. I need to be able to just add a row each time they enter the sub.

Re: Array Problem: Object reference not set to an instance of an object.

Posted 11 January 2012 - 01:14 PM

An array isn't meant to be resized so when you use the redim you are really just creating a new array, and copying everything over to it. A list, or a collection for that matter is easier to size because it is designed to be, where as an array is not.