[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
295 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 1000 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
Technology Partners: 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 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

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.

Question has a verified solution.

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

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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline

834 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