Results 1 to 5 of 5

Thread: [02/03] Object reference not set to an instance of an object Error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    [02/03] Object reference not set to an instance of an object Error

    Hi... I am getting the following error:
    An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll

    Additional information: Object reference not set to an instance of an object.
    I am trying to copy a listview collection to an array. I want the array to be string but then I get another error if I try to copy listview.items to a string array

    Code:
    Dim sourceArray() As Object
    frm1.lstRight.Items.CopyTo(sourceArray, 0)

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [02/03] Object reference not set to an instance of an object Error

    You have to set/specify the size of the Object array first of all. You might be able to do something like this:

    vb Code:
    1. Dim sourceArray(Me.ListView1.Items.Count -1) As Object 'Creates correct size of array
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [02/03] Object reference not set to an instance of an object Error

    Quote Originally Posted by jeffnyc
    Code:
    frm1.lstRight.Items.CopyTo(sourceArray, 0)
    Is frm1 the actual name of the form, or the current instance? If frm1 is the name of the form then that is your problem. Also, is this the line that throws the error?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    Re: [02/03] Object reference not set to an instance of an object Error

    Quote Originally Posted by circuits2
    Is frm1 the actual name of the form, or the current instance? If frm1 is the name of the form then that is your problem. Also, is this the line that throws the error?

    frm1 is the instance, i believe... fm1 is declared in a module and when form1 loads frm1 = me

    i believe it is the way i am copying listview collection of objects to a standard array and in the way i declare it -

    haven't tried stimbo's suggestion but also wondering if i need the NEW keyword anywhere

  5. #5
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [02/03] Object reference not set to an instance of an object Error

    Ok, you need to change it to this:

    Me.lstRight.Items.CopyTo(sourceArray, 0)
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width