?
Solved

Object reference not set to an instance of an object

Posted on 2005-03-06
6
Medium Priority
?
158 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Starting up a Project
Suggested Courses

777 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