[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register 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
?
294 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

650 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