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

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 7512 Views - Last Post: 11 January 2012 - 03:16 PM Rate Topic: -----

#1 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

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.

this is where i define the arrays (m_strFiles and m_strCheck):
Option Explicit On
Option Strict On

Module modMain

    Private m_strFilename As String
    Private m_Files() As String
    Private m_strCheck(,,) As String

    Sub Main()


and this is the best example of the failing code:

Sub Load()
        Dim di As New IO.DirectoryInfo(System.IO.Directory.GetCurrentDirectory & "\BudgetSheets\")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo
        Dim x, y, z As Integer

        Console.Clear()
        Console.WriteLine(Space(4) & "**********")
        Console.WriteLine(Space(4) & "***LOAD***")
        Console.WriteLine(Space(4) & "**********")
        Console.WriteLine()
        For Each dra In diar1
            x += 1
            m_Files(x) = dra.ToString
            Console.Write(x & ". ")
            Console.Write(m_Files(x))
            Console.Write(vbNewLine)
        Next
        Console.WriteLine()
        Console.Write("Please select file to open: ")
        x = CInt(Console.ReadLine)
        m_strFilename = System.IO.Directory.GetCurrentDirectory & "\BudgetSheets\" & m_Files(x)

        Dim StrSplit(2), LineRead As String
        Dim sreReader As IO.StreamReader
        sreReader = IO.File.OpenText(m_strFilename)
        x = 0
        y = 0
        z = 0
        Do
            LineRead = sreReader.ReadLine
            If LineRead = String.Empty Then
                Console.WriteLine(m_strFilename & " loaded...")
                Console.WriteLine()
                Console.WriteLine("Press any key to continue...:")
                Console.ReadLine()
                Call Menu()
            End If
            StrSplit = LineRead.Split(CChar(","))
            m_strCheck(x, y, z) = StrSplit(0)
            y += 1
            m_strCheck(x, y, z) = StrSplit(1)
            z += 1
            m_strCheck(x, y, z) = StrSplit(2)
            x += 1
            y = 0
            z = 0
        Loop Until sreReader.Peek = -1
        sreReader.Close()
        Console.WriteLine()
        Console.WriteLine(m_strFilename & " loaded...")
        Console.WriteLine()
        Console.WriteLine("Press any key to continue...")
        Console.ReadLine()
        Call Menu()
    End Sub


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

Is This A Good Question/Topic? 0
  • +

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

#2 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

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

Posted 10 January 2012 - 11:09 PM

View PostTechnoBear, 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.

this is where i define the arrays (m_strFiles and m_strCheck):
Option Explicit On
Option Strict On

Module modMain

    Private m_strFilename As String
    Private m_Files() As String
    Private m_strCheck(,,) As String

    Sub Main()


and this is the best example of the failing code:

Sub Load()
        Dim di As New IO.DirectoryInfo(System.IO.Directory.GetCurrentDirectory & "\BudgetSheets\")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo
        Dim x, y, z As Integer

        Console.Clear()
        Console.WriteLine(Space(4) & "**********")
        Console.WriteLine(Space(4) & "***LOAD***")
        Console.WriteLine(Space(4) & "**********")
        Console.WriteLine()
        For Each dra In diar1
            x += 1
            m_Files(x) = dra.ToString
            Console.Write(x & ". ")
            Console.Write(m_Files(x))
            Console.Write(vbNewLine)
        Next
        Console.WriteLine()
        Console.Write("Please select file to open: ")
        x = CInt(Console.ReadLine)
        m_strFilename = System.IO.Directory.GetCurrentDirectory & "\BudgetSheets\" & m_Files(x)

        Dim StrSplit(2), LineRead As String
        Dim sreReader As IO.StreamReader
        sreReader = IO.File.OpenText(m_strFilename)
        x = 0
        y = 0
        z = 0
        Do
            LineRead = sreReader.ReadLine
            If LineRead = String.Empty Then
                Console.WriteLine(m_strFilename & " loaded...")
                Console.WriteLine()
                Console.WriteLine("Press any key to continue...:")
                Console.ReadLine()
                Call Menu()
            End If
            StrSplit = LineRead.Split(CChar(","))
            m_strCheck(x, y, z) = StrSplit(0)
            y += 1
            m_strCheck(x, y, z) = StrSplit(1)
            z += 1
            m_strCheck(x, y, z) = StrSplit(2)
            x += 1
            y = 0
            z = 0
        Loop Until sreReader.Peek = -1
        sreReader.Close()
        Console.WriteLine()
        Console.WriteLine(m_strFilename & " loaded...")
        Console.WriteLine()
        Console.WriteLine("Press any key to continue...")
        Console.ReadLine()
        Call Menu()
    End Sub


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.

This post has been edited by trevster344: 10 January 2012 - 11:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

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

Posted 10 January 2012 - 11:13 PM

I've never gotten issues like you, sounds interesting though, I can declare arrays like that and not have errors - might investigate :)
To TechnoBear: what line is it giving you? In the error I mean.

This post has been edited by jimmyBo: 10 January 2012 - 11:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

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

Posted 10 January 2012 - 11:24 PM

@jimmyBo: the code break happens at the
m_Files(x) = dra.ToString
line.

@trevster344: I tried to define the array as list(of String) but am unable to convert string to list(of string)
Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

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.
Was This Post Helpful? 1
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9201
  • View blog
  • Posts: 34,571
  • Joined: 12-June 08

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.
Was This Post Helpful? 0
  • +
  • -

#7 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

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

Posted 10 January 2012 - 11:41 PM

Yea, I ran into this problem quite a bit recently. Kept forgetting to declare my arrays properly.
Was This Post Helpful? 0
  • +
  • -

#8 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

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

Posted 10 January 2012 - 11:50 PM

View Posttrevster344, on 10 January 2012 - 11:32 PM, said:

you can even use redim and change the size later on.


I was using redim before to set it as I am reading in from files and do not know what the array size is required to be, however redim ended up clearing all but the last item in the array.

View Postmodi123_1, on 10 January 2012 - 11:33 PM, said:

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...


I have the array start at 1 instead of zero as the item in the array is chosen through a number selection from 1 to whatever the final number ends up being.

does explain why it would get through that section when i had the redim in but doesnt solve the bigger problem as redim seems to clear previously entered information
Was This Post Helpful? 0
  • +
  • -

#9 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

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.
Was This Post Helpful? 0
  • +
  • -

#10 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

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

Posted 11 January 2012 - 09:25 AM

There is a method of Redim that you use to keep all current data. I don't know it by name but I'll find it when I get to my work station
Was This Post Helpful? 0
  • +
  • -

#11 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

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

Posted 11 January 2012 - 09:40 AM

Thank you that would be handy
Was This Post Helpful? 0
  • +
  • -

#12 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

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

Posted 11 January 2012 - 09:53 AM

View Posttrevster344, on 11 January 2012 - 10:25 AM, said:

There is a method of Redim that you use to keep all current data. I don't know it by name but I'll find it when I get to my work station


Try using the preserve keyword after Redim
Redim Preserve K(25)


Was This Post Helpful? 0
  • +
  • -

#13 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

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

Posted 11 January 2012 - 10:18 AM

tried using preserve and got the error:

An unhandled exception of type 'System.ArrayTypeMismatchException' occurred in Microsoft.VisualBasic.dll

Additional information: 'ReDim' can only change the rightmost dimension.

Was This Post Helpful? 0
  • +
  • -

#14 TechnoBear  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 222
  • Joined: 02-November 11

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

Posted 11 January 2012 - 10:43 AM

the dimension I am trying to change is the left most.
Was This Post Helpful? 0
  • +
  • -

#15 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

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.

Public Sub ListMumbo()
Dim k as new List(Of Integer)
Dim l(1) as integer
dim j as integer = 12

l(0) = 1
l(1) = 2

k.add(l(0))
k.add(l(1))
k.add(j)

for each int1 as integer in k
Messagebox.show(int1 & " blam!")
loop

End Sub


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2