Link to home
Create AccountLog in
Avatar of StowHigh
StowHigh

asked on

Object reference not set to an instance of an object

I am writing a very basic program (I'm relatively new to VB .Net) and am receiving the above message
This is the declaration
Public Class Alert
    Inherits System.Windows.Forms.Form
    Dim ads, ads1, ads2, ads3, ads4 As DataSet
    Dim SameSw As Boolean = False
    'Internal array for code drops
    Dim DropData(200) As String
    Dim DropComp(50) As String
This is a portion called routine

Sub AddSyncComp(ByVal StringPos As Integer)
        Dim SyncCtr As Integer
        'Add first component
        DropComp(DropCtr) = TargetEnv.Substring(8, 5)
        DropCtr = DropCtr + 1
        For SyncCtr = 13 To StringPos

MSDN is not helpful in recommending a potential solution

Any ideas?
Avatar of fatalXception
fatalXception

Where and when does this error occur?
The code portion you have posted doesn't seem complete?
The error message itself is indicating that at some point, you declared a variable as some kind of object, but did not actually point it at an object.

e.g, this would cause a similar error:
dim thing as myObject
thing.someMethod()   <--Error here because thing was not SET, it's NULL at the moment

correct usage would be
dim thing as myObject
set thing = new myObject
thing.someMethod

so can you check your code for possible issues like this, and also run this in debug mode, and post back the code where this occurs?
the problem appears to be on this line:

DropComp(DropCtr) = TargetEnv.Substring(8, 5)


Since you have declared an ARRAY of strings, but never created an instance of any string.  Also, where is the variable DropCtr initiallized?  What is its value in this routine?

AW
Avatar of StowHigh

ASKER

Arthur,

You are correct in identifying the problem statement, but I am more confused than ever now.

I can create a skeletal program which has nothing more than this
Public Class Alert
    Inherits System.Windows.Forms.Form
Dim Ctr As Integer = 0
Dim DropComp(50) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Droptest()
End Sub
Private Sub Droptest()
DropComp(Ctr) = TextBox1.Text.Substring(8, 5)
End Sub

Without getting any error.  I can't see the difference


ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Arthur,

Everything you are saying is logical, but this is basically the way things are declared

Public Class Alert
    Inherits System.Windows.Forms.Form
    Dim ads, ads1, ads2, ads3, ads4 As DataSet
    Dim SameSw As Boolean = False
    'Internal array for code drops
    Dim DropData(200) As String
    Dim DropComp(50) As String
    Dim DropCtr As Integer = 0
    Dim PosCtr As Integer = 3
.
.
.
 For StringCtr = 7 To (Len(TargetEnv) - 1)
                If (Len(TargetEnv) - StringCtr) >= 2 AndAlso TargetEnv.Substring(StringCtr, 2) = "S " Then
                    StringSync = StringCtr
                    If StringSync > 0 Then
                        AddSyncComp(StringSync)
                    End If
.
.
.
Private Sub AddSyncComp(ByVal StringPos As Integer)
        Dim SyncCtr As Integer
        'Add first component
        MsgBox(DropCtr)
        DropComp(DropCtr) = TargetEnv.Substring(8, 5)
        DropCtr = DropCtr + 1
.
.
.
I am still getting the error
Find the Error Line and put a breakpoint on it should be the easiest way.