?
Solved

Object reference not set to an instance of an object

Posted on 2005-03-06
6
Medium Priority
?
162 Views
Last Modified: 2013-11-25
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?
0
Comment
Question by:StowHigh
6 Comments
 
LVL 8

Expert Comment

by:fatalXception
ID: 13472041
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?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 13472697
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
0
 

Author Comment

by:StowHigh
ID: 13473394
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


0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 2000 total points
ID: 13473758
because you set the value of CTR to 0, and the array has a default 0-th value.  

Dim Ctr As Integer = 0  ' this line being Module level, is accessible to all the other code in the class
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

It would appear that, in the code that triggers the error, you have NOT declared the variable DropCtr, and that is the variable that is triggering the error.  Try declaring that variable, and intializing it to 0, and see if that resolves the issue.

AW
0
 

Author Comment

by:StowHigh
ID: 13476520
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
0
 
LVL 3

Expert Comment

by:inxni
ID: 13543465
Find the Error Line and put a breakpoint on it should be the easiest way.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Why WooCommerce is one of the majorly favored choices when it comes to having an eCommerce store. This article will acquaint you with some reasons that I believe make it one of the best eCommerce platforms available.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question