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
287 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Data Tools for Visual Studio 2015 - backward compatibility. 2 111
DataGridView Events ? 3 47
Regex validation 2 25
start a process from a service 3 23
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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