DataGridViewComboBoxColumn Error (Object reference not set to an instance of an object)

Posted on 2007-07-29
Last Modified: 2012-06-21
VB 2005, Windows Forms

Here is my code:

        Dim nColCombo1 As New DataGridViewComboBoxColumn
        With nColCombo1
            .HeaderText = "Item"
            .Name = "Col_ItemID"
            Dim x As String = MyItem.FillItemsGrid(nColCombo1) ' Rise Error
        End With

In my class :

   Public Function FillItemsGrid(ByRef cBox As Object, Optional ByVal ProvId As Integer = 0) As String
        Dim Sqls As String

My Problem is the error was rised when calling the function ( Object reference not set to an instance of an object . )

Thanx in advance
Question by:hatem72
    LVL 3

    Expert Comment

    I think your problem might be with passing a reference type (object) by reference. Almost always an object should be passed by the default ByVal. You will still be able to change the properties of the object, since ByVal passes a copy of the pointer to the object and not the actual pointer to the object (for reference types such as objects). The danger of passing the original pointer to the object, (ByRef) if not needed is that the method can replace the object with a different instance, which is what I think may be happening.
    LVL 6

    Author Comment


    First thank u ... i did try to change ByRef -> ByVal   BUT Same Problem

    ok is there any way to pass DataGridViewComboBoxColumn to Class ???
    instead of my way
    LVL 34

    Accepted Solution

    I assume MyItem is declared AND instantiated?  E,g, Dim MyItem As New MyClass.  And within scope for the code you show.

    LVL 6

    Author Comment


    Yes Sancler ... MyItem class works fine and there is many functions and subs and all works fine.
    LVL 3

    Assisted Solution

    Your way should work. Just add an includes for the System.Wihdows.Forms  to give access to the DataGridViewComboBoxColumn and type your cbox as that, then use the object passed, even ByVal, the members can be accessed on the original object, so you can populate it.
    LVL 6

    Author Comment



    Sancler u r right about the problem ... i am sure that is the class is fine but i dont know
    how do i miss the NEW keyword ... after ur post i copy the function form the class
    to the form and it works ... so i found where the problem is .

    lorelogic thank u for ur help ... SORYY there is just 500p to split .

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Suggested Solutions

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 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

    19 Experts available now in Live!

    Get 1:1 Help Now