Dir() Error

sn1fflez

Newcomer
Joined
Jul 30, 2010
Messages
8
Hello Everyone,

I have some code (which you can find below) and I am able to compile it correctly on my computer but when I copy and paste it into another computer (for further development); I get the error: "Object reference not set to an instance of an object". I have provided a screen shot of the error and the pertinent code below. As you can see from the screen shot the error occurs in the line: " If Dir(pfile(j)) = "" Then".
Code:
Sub ChartPlot()
        Dim i As Integer
        Dim j As Integer
        Dim KF As Integer
        Dim J1 As Integer
        Dim J2 As Integer
        Dim JLG As Integer
        Dim NPTS As Integer
        Dim IPLOT(4, 5) As Object   'changed from integer
        Dim ICC(5) As Integer
        Dim ICLOSE As Integer
        Dim ICONX As Integer
        Dim FILEGRAPH As String
        Dim pfile(5) As String
        Dim Lgnd(5) As String
        'Dim VP1!(), VPC1!()    Double Precision change
        Dim VP1(,) As Double
        Dim VPC1(,) As Double
        Dim Crve As MSChart20Lib.Series
        'Dim VL!()
        Dim VL() As Double

        'On Error GoTo 200 'removed for debugging
        IREADT = 0
        'Read Table Data
        VSFlexGrid1.Row = 0
        For j = 1 To 5
            VSFlexGrid1.Col = j
            pfile(j) = VSFlexGrid1.Text
           If Dir(pfile(j)) = "" Then
                Mss(1) = "File  " & pfile(j) & "  can ot be found"
                Call ErrMessF()
                Exit Sub
            End If
        Next
end sub

I can't for the life of me figure out why the code works on one computer and not on another. Any help will be greatly appreciated.
 

Attachments

The first question you should be asking yourself when dealing with a NullReferenceException is which variable (or object refernce) is null (i.e. "Nothing"). Look at the code:
Code:
           If Dir([COLOR="Red"]pfile(j)[/COLOR]) = "" Then
There are two references used in the line of code. A reference to the array pfile, and reference to an object within the array, pfile(j).

You can use the debugger to examine the array and its contents to see which reference is null. You would discover that pfile is not null, but pfile(j) returns a null reference (i.e. Nothing). To figure out why pfile(j) is null, you need to consider all the places that the value can be assigned. In this case, pfile(j) is only assigned in one place:
Code:
[COLOR="Red"]pfile(j) =[/COLOR] VSFlexGrid1.Text
This must be where pfile(j) is set to null, because this is the only place pfile(j) is ever set. This means that VSFlexGrid1.Text is returning a null reference.
 
Hey Snarfblam,

Thanks for schooling me again. You definitely saved my life. I really appreciate the time you took to explain in detail how I should approach these kinds of problems.

Sn1fflez
 
Back
Top