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
286 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
System32Int Error 8 57
VB.NET HttpWebRequest 12 58
vb.net - How to check if current user is an administrator? 6 35
VB.Net - Windows UserName index and length error 4 22
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

21 Experts available now in Live!

Get 1:1 Help Now