Solved

array variable input creats a run time error:" Object reference not set to an instance of an object

Posted on 2011-03-16
10
285 Views
Last Modified: 2012-05-11
Hello Experts, I ran into an odd problem..
I have a public  array in my project:  Public COCvals As Long() = {0, 0, 0, 0}
when trying to run this code line: COCvals(0) = 1
 I get a run time error :" Object reference not set to an instance of an object "
 this never happened until today when I added a new component to my project. the com dll is a resize solution. it uses .net framework2. I am using v4. when I delete the component from my form the error dissapears.
what could it be?
 
0
Comment
Question by:Hagita
  • 6
  • 4
10 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35148322
I would guess the error is referring to the fact that your resize component hasn't been instantiated.  The component's written in .Net 2 - so you're adding a regular .Net reference, or are you adding it as a COM reference?
0
 

Author Comment

by:Hagita
ID: 35148372
I didnt make myself clear enough:
it is a toolbox item. I added it as a .net component and drew it on my form. no declaration needed.
I know its starnge but the error only appears when the code line is array input. if i mark it everything is ok.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 250 total points
ID: 35148480
Then there's an issue with your component - perhaps a required property isn't set, it's got a bug, etc.

These lines of code will never produce that error:
Dim COCvals As Long() = {0, 0, 0, 0}
COCvals(0) = 1

Open in new window


However, if something else sets COCvals to Nothing in between those lines, you will get that error:
Dim COCvals As Long() = {0, 0, 0, 0}
COCvals = Nothing
COCvals(0) = 1

Open in new window


Also, if you're passing that Long() to a method on that component, then that component could be throwing that exception if it's note expecting a Long() (e.g. it wants a string, not a Long array).  Consider this silly example, imagine the class SomeComponent is your resize componenet:
Module Module1

    Sub Main()
        Dim component As New SomeComponent()
        Dim COCvals As Long() = {0, 0, 0, 0}
        Dim stringVal As String = "0, 0, 0, 0"

        'component.SomeMethod works when passing a string
        component.SomeMethod(stringVal)

        'But throws an exception if you try to pass in anything not a string
        component.SomeMethod(COCvals)

        Console.ReadKey()
    End Sub

End Module

Class SomeComponent
    Public Sub SomeMethod(ByVal arg As Object)
        'arg is declared as an object, so you will be able to pass
        'in anything you want, but TryCast is going to
        'try and assign it to a string variable - if
        'arg is not a string (or is not implicitly convertable to a string)
        'then TryCast returns Nothing, in which case we'll see the error
        Dim message As String = TryCast(arg, String)
        Console.WriteLine("Message length: " & message.Length)
    End Sub
End Class

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35148489
The stack trace from your exception might help narrow down the problem.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35148698
I forgot to ask what component is it - something I can download & test?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Hagita
ID: 35148709

  at winCOCapp.frmCOCmain.ShowData() in C:\Documents and Settings\hagita\My Documents\Visual Studio 2010\Projects\COCapp16032011\COCapp\frmCOCmain.vb:line 914"

 the line is:    COCvals(0) = CLng(txtWorkNo.Text)
0
 

Author Comment

by:Hagita
ID: 35148730
yes please:
the component is Softgroup.NetResize.dll.
can be downloded for trial  at;

http://www.soft-group.it/net
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35148968
Are you able to post the complete code for your form, frmCOCmain.vb?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35149340
Actually, looking a little closer at the error, COCvals(0) = CLng(txtWorkNo.Text), would indicate that txtWorkNo is Nothing. Are you adding controls manually in code, at runtime?  I am unable to duplicate your issue - I don't think it really has anything to do with the NetResize control.  Do you have code in any of the NetResize events?

Public Class Form1
    Public COCvals() As Long = {0, 0, 0, 0}
    Private txtWorkNo As TextBox
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'txtWorkNo is declared, but never instantiated with the "New" keyword
        COCvals(0) = CLng(txtWorkNo.Text)
    End Sub
End Class

Open in new window


On a slightly unrelated note - you might consider using Long.TryParse to test whether or not the text in the text box can be converted to a Long, this will save you from an InvalidCastException if someone types "Hello World" instead of a number.  The "Long" keyword in VB is just a synonym for Int64, the two can be used interchangably - http://msdn.microsoft.com/en-us/library/zc2x2b1h.aspx
Public Class Form1
    Public COCvals() As Long = {0, 0, 0, 0}

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim newCocVal As Long
        If Not Long.TryParse(txtWorkNo.Text, newCocVal) Then
            'If TryParse returns False, then the text box's value
            'couldn't be converted to a Long
            MessageBox.Show("Invalid entry in txtWorkNo.")
            Exit Sub
        End If

        'Otherwise, if TryParse returns True, then newCocVal
        'contains the converted value of the text box
        COCvals(0) = newCocVal
        MessageBox.Show("Continuing normal processing with value: " & COCvals(0))
    End Sub
End Class

Open in new window

0
 

Author Comment

by:Hagita
ID: 35154308
Problem resolved. I found a code line that set the array to nothing on the Dispose sub of the designer.
Thank you for your efforts.
Hagit
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now