Current location: Hot Scripts Forums » Programming Languages » Windows .NET Programming » Object reference not set to an instance of an object.


Object reference not set to an instance of an object.

Reply
  #1 (permalink)  
Old 04-08-09, 03:34 AM
m.timoney's Avatar
m.timoney m.timoney is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Leicester - UK
Posts: 76
Thanks: 1
Thanked 0 Times in 0 Posts
Question Object reference not set to an instance of an object.

Okay i'm trying to run the following line of code but it is throwing a null reference error
Code:
lvi = lvSummary.Items.Add(item.ToLVI)

now normally it works but occasionally it throw the exception and for the life of me i can't figure out why 90% of the time it works and 10% it fails

here is a dump of the watches on the 3 items involved in the line, can someone tell me what the problem is and why it is happening?

Code:
-        lvi    {Text = ""}    System.Windows.Forms.ListViewItem
+        BackColor    "{Name=Window, ARGB=(255, 255, 255, 255)}"    System.Drawing.Color
+        Bounds    {X = 0 Y = 0 Width = 0 Height = 0}    System.Drawing.Rectangle
        Checked    False    Boolean
        Focused    False    Boolean
+        Font    {Name = "Microsoft Sans Serif" Size=8.25}    System.Drawing.Font
+        ForeColor    "{Name=WindowText, ARGB=(255, 0, 0, 0)}"    System.Drawing.Color
        Group    Nothing    System.Windows.Forms.ListViewGroup
        ImageIndex    -1    Integer
        ImageKey    ""    String
        ImageList    Nothing    System.Windows.Forms.ImageList
        IndentCount    0    Integer
        Index    -1    Integer
        ListView    Nothing    System.Windows.Forms.ListView
        Name    ""    String
+        Position    {X = -1 Y = -1}    System.Drawing.Point
        Selected    False    Boolean
        StateImageIndex    -1    Integer
+        SubItems    {System.Windows.Forms.ListViewItem.ListViewSubItemCollection}    System.Windows.Forms.ListViewItem.ListViewSubItemCollection
        Tag    Nothing    Object
        Text    ""    String
        ToolTipText    ""    String
        UseItemStyleForSubItems    True    Boolean
_____________________________________________________________

-        lvSummary    {System.Windows.Forms.ListView}    System.Windows.Forms.ListView
+        AccessibilityObject    {"Cannot access a disposed object. Object name: 'ListView'."}    System.Windows.Forms.AccessibleObject
        AccessibleDefaultActionDescription    Nothing    String
        AccessibleDescription    Nothing    String
        AccessibleName    Nothing    String
        AccessibleRole    Default {-1}    System.Windows.Forms.AccessibleRole
        Activation    Standard {0}    System.Windows.Forms.ItemActivation
        Alignment    Top {2}    System.Windows.Forms.ListViewAlignment
        AllowColumnReorder    False    Boolean
        AllowDrop    False    Boolean
        Anchor    5    System.Windows.Forms.AnchorStyles
        AutoArrange    True    Boolean
+        AutoScrollOffset    {X = 0 Y = 0}    System.Drawing.Point
        AutoSize    False    Boolean
+        BackColor    "{Name=Window, ARGB=(255, 255, 255, 255)}"    System.Drawing.Color
        BackgroundImage    Nothing    System.Drawing.Image
        BackgroundImageLayout    Tile {1}    System.Windows.Forms.ImageLayout
        BackgroundImageTiled    False    Boolean
        BindingContext    Nothing    System.Windows.Forms.BindingContext
        BorderStyle    Fixed3D {2}    System.Windows.Forms.BorderStyle
        Bottom    220    Integer
+        Bounds    {X = 0 Y = 25 Width = 390 Height = 195}    System.Drawing.Rectangle
        CanFocus    False    Boolean
        CanSelect    False    Boolean
        Capture    False    Boolean
        CausesValidation    True    Boolean
        CheckBoxes    False    Boolean
+        CheckedIndices    {System.Windows.Forms.ListView.CheckedIndexCollection}    System.Windows.Forms.ListView.CheckedIndexCollection
+        CheckedItems    {System.Windows.Forms.ListView.CheckedListViewItemCollection}    System.Windows.Forms.ListView.CheckedListViewItemCollection
        CheckForIllegalCrossThreadCalls    True    Boolean
+        ClientRectangle    {X = 0 Y = 0 Width = 386 Height = 191}    System.Drawing.Rectangle
+        ClientSize    {Width = 386 Height = 191}    System.Drawing.Size
+        Columns    {System.Windows.Forms.ListView.ColumnHeaderCollection}    System.Windows.Forms.ListView.ColumnHeaderCollection
        CompanyName    "Microsoft Corporation"    String
        Container    Nothing    System.ComponentModel.IContainer
        ContainsFocus    False    Boolean
        ContextMenu    Nothing    System.Windows.Forms.ContextMenu
        ContextMenuStrip    Nothing    System.Windows.Forms.ContextMenuStrip
+        Controls    {System.Windows.Forms.Control.ControlCollection}    System.Windows.Forms.Control.ControlCollection
        Created    False    Boolean
+        Cursor    {System.Windows.Forms.Cursor}    System.Windows.Forms.Cursor
+        DataBindings    {System.Windows.Forms.ControlBindingsCollection}    System.Windows.Forms.ControlBindingsCollection
+        DefaultBackColor    "{Name=Control, ARGB=(255, 212, 208, 200)}"    System.Drawing.Color
+        DefaultFont    {Name = "Microsoft Sans Serif" Size=8.25}    System.Drawing.Font
+        DefaultForeColor    "{Name=ControlText, ARGB=(255, 0, 0, 0)}"    System.Drawing.Color
+        DisplayRectangle    {X = 0 Y = 0 Width = 386 Height = 191}    System.Drawing.Rectangle
        Disposing    False    Boolean
        Dock    Fill {5}    System.Windows.Forms.DockStyle
        Enabled    True    Boolean
        Focused    False    Boolean
        FocusedItem    Nothing    System.Windows.Forms.ListViewItem
+        Font    {Name = "Microsoft Sans Serif" Size=8.25}    System.Drawing.Font
+        ForeColor    "{Name=WindowText, ARGB=(255, 0, 0, 0)}"    System.Drawing.Color
        FullRowSelect    True    Boolean
        GridLines    False    Boolean
+        Groups    {System.Windows.Forms.ListViewGroupCollection}    System.Windows.Forms.ListViewGroupCollection
+        Handle    {"Cannot access a disposed object. Object name: 'ListView'."}    System.IntPtr
        HasChildren    False    Boolean
        HeaderStyle    Clickable {2}    System.Windows.Forms.ColumnHeaderStyle
        Height    195    Integer
        HideSelection    False    Boolean
        HotTracking    False    Boolean
        HoverSelection    False    Boolean
        ImeMode    NoControl {0}    System.Windows.Forms.ImeMode
+        InsertionMark    {System.Windows.Forms.ListViewInsertionMark}    System.Windows.Forms.ListViewInsertionMark
        InvokeRequired    False    Boolean
        IsAccessible    False    Boolean
        IsDisposed    True    Boolean
        IsHandleCreated    False    Boolean
        IsMirrored    False    Boolean
+        Items    {System.Windows.Forms.ListView.ListViewItemCollection}    System.Windows.Forms.ListView.ListViewItemCollection
        LabelEdit    False    Boolean
        LabelWrap    True    Boolean
        LargeImageList    Nothing    System.Windows.Forms.ImageList
+        LayoutEngine    {System.Windows.Forms.Layout.DefaultLayout}    System.Windows.Forms.Layout.LayoutEngine
        Left    0    Integer
+        ListViewItemSorter    {Presenter.Utilities.ListViewColumnSorter}    System.Collections.IComparer
+        Location    {X = 0 Y = 25}    System.Drawing.Point
+        Margin    {System.Windows.Forms.Padding}    System.Windows.Forms.Padding
+        MaximumSize    {Width = 0 Height = 0}    System.Drawing.Size
+        MinimumSize    {Width = 0 Height = 0}    System.Drawing.Size
        ModifierKeys    None {0}    System.Windows.Forms.Keys
        MouseButtons    None {0}    System.Windows.Forms.MouseButtons
+        MousePosition    {X = 43 Y = 875}    System.Drawing.Point
        MultiSelect    True    Boolean
        Name    "lvSummary"    String
        OwnerDraw    False    Boolean
+        Padding    {System.Windows.Forms.Padding}    System.Windows.Forms.Padding
        Parent    Nothing    System.Windows.Forms.Control
+        PreferredSize    {Width = 0 Height = 0}    System.Drawing.Size
        ProductName    "Microsoft® .NET Framework"    String
        ProductVersion    "2.0.50727.3053"    String
        RecreatingHandle    False    Boolean
        Region    Nothing    System.Drawing.Region
        Right    390    Integer
        RightToLeft    No {0}    System.Windows.Forms.RightToLeft
        RightToLeftLayout    False    Boolean
        Scrollable    True    Boolean
+        SelectedIndices    {System.Windows.Forms.ListView.SelectedIndexCollection}    System.Windows.Forms.ListView.SelectedIndexCollection
+        SelectedItems    {System.Windows.Forms.ListView.SelectedListViewItemCollection}    System.Windows.Forms.ListView.SelectedListViewItemCollection
        ShowGroups    True    Boolean
        ShowItemToolTips    False    Boolean
        Site    Nothing    System.ComponentModel.ISite
+        Size    {Width = 390 Height = 195}    System.Drawing.Size
        SmallImageList    Nothing    System.Windows.Forms.ImageList
        Sorting    None {0}    System.Windows.Forms.SortOrder
        StateImageList    Nothing    System.Windows.Forms.ImageList
        TabIndex    7    Integer
        TabStop    True    Boolean
        Tag    Nothing    Object
        Text    ""    String
+        TileSize    {Width = 0 Height = 0}    System.Drawing.Size
        Top    25    Integer
+        TopItem    {"Object reference not set to an instance of an object."}    System.Windows.Forms.ListViewItem
        TopLevelControl    Nothing    System.Windows.Forms.Control
        UseCompatibleStateImageBehavior    False    Boolean
        UseWaitCursor    False    Boolean
        View    Details {1}    System.Windows.Forms.View
        VirtualListSize    0    Integer
        VirtualMode    False    Boolean
        Visible    False    Boolean
        Width    390    Integer
+        WindowTarget    {System.Windows.Forms.Control.ControlNativeWindow}    System.Windows.Forms.IWindowTarget
____________________________________________________________________

-        item.ToLVI    {Text = "Doe, Jane"}    System.Windows.Forms.ListViewItem
+        BackColor    "{Name=Window, ARGB=(255, 255, 255, 255)}"    System.Drawing.Color
+        Bounds    {X = 0 Y = 0 Width = 0 Height = 0}    System.Drawing.Rectangle
        Checked    False    Boolean
        Focused    False    Boolean
+        Font    {Name = "Microsoft Sans Serif" Size=8.25}    System.Drawing.Font
+        ForeColor    "{Name=WindowText, ARGB=(255, 0, 0, 0)}"    System.Drawing.Color
        Group    Nothing    System.Windows.Forms.ListViewGroup
        ImageIndex    -1    Integer
        ImageKey    ""    String
        ImageList    Nothing    System.Windows.Forms.ImageList
        IndentCount    0    Integer
        Index    -1    Integer
        ListView    Nothing    System.Windows.Forms.ListView
        Name    ""    String
+        Position    {X = -1 Y = -1}    System.Drawing.Point
        Selected    False    Boolean
        StateImageIndex    -1    Integer
+        SubItems    {System.Windows.Forms.ListViewItem.ListViewSubItemCollection}    System.Windows.Forms.ListViewItem.ListViewSubItemCollection
        Tag    Nothing    Object
        Text    "Doe, Jane"    String
        ToolTipText    ""    String
        UseItemStyleForSubItems    True    Boolean
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.

Last edited by m.timoney; 04-08-09 at 04:00 AM.
Reply With Quote
  #2 (permalink)  
Old 04-10-09, 06:00 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Hi,

Any chance you can give us some more code. Preferably the whole function?
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #3 (permalink)  
Old 04-14-09, 03:52 AM
m.timoney's Avatar
m.timoney m.timoney is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Leicester - UK
Posts: 76
Thanks: 1
Thanked 0 Times in 0 Posts
i managed to find the answer, it was due to an event handler not being properly disposed of.

so a disposed form was running a handler but as it had been disposed it couldn't run the handler
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.
Reply With Quote
  #4 (permalink)  
Old 04-14-09, 03:55 AM
m.timoney's Avatar
m.timoney m.timoney is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Leicester - UK
Posts: 76
Thanks: 1
Thanked 0 Times in 0 Posts
i spotted that the 10% was a 100% of the time you closed the form and then opened it again

thanks for the help
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.
Reply With Quote
  #5 (permalink)  
Old 04-14-09, 04:33 AM
m.timoney's Avatar
m.timoney m.timoney is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Leicester - UK
Posts: 76
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Yeroon View Post
Hi,

Any chance you can give us some more code. Preferably the whole function?
even though i've found the answer the rest of the non designer class was
Code:
Namespace Controls.Misc
    Public Class BlankSummary
        Public Property ShowControls() As Boolean
            Get
                Return ToolStrip1.Visible
            End Get
            Set(ByVal value As Boolean)
                ToolStrip1.Visible = value
            End Set
        End Property
        Public Property ShowDeleted() As Boolean
            Get
                Return tsCbShowDel.Checked
            End Get
            Set(ByVal value As Boolean)
                tsCbShowDel.Checked = value
            End Set
        End Property

        Public Property ShowLabel() As Boolean
            Get
                Return tsLblTxt.Visible
            End Get
            Set(ByVal value As Boolean)
                tsLblTxt.Visible = value
            End Set
        End Property

        Public Property LabelText() As String
            Get
                Return tsLblTxt.Text
            End Get
            Set(ByVal value As String)
                tsLblTxt.Text = value
            End Set
        End Property


        Protected ItemSelector As New Hashtable
        Sub ItemAdd(ByVal item As Bases.Base_Archive)
            If ShowDeleted Or item.Active Then

                Dim lvi As New ListViewItem
                lvi = lvSummary.Items.Add(item.ToLVI)
                ItemSelector.Add(lvi, item)
            End If

        End Sub
        Sub ItemRemove(ByVal item As ListViewItem)
            ItemSelector.Remove(item)
            lvSummary.Items.Remove(item)
        End Sub
        Sub ItemReset()
            ItemSelector.Clear()
            lvSummary.Items.Clear()
        End Sub

        Protected bRefreshing As Boolean = False
        Public Overridable Sub RefreshData()
            bRefreshing = True

            Try
                lvSummary.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
            Catch ex As Exception

            End Try

            If Not tsCbShowDel.Checked Then
                colDel.Width = 0
            End If
            lvSummary.Sort()

            bRefreshing = False
        End Sub

        Private oSelected As Object
        Public Property SelectedObject() As Object
            Get
                Return oSelected
            End Get
            Set(ByVal value As Object)
                oSelected = value

                RefreshData()

            End Set
        End Property

        Public Event SelectedChanged(ByVal NewValue As Object)
        Public Event AddItem()

        Public Sub AddButton(ByVal item As ToolStripItem)
            tsButAdd.DropDownItems.Add(item)
            tsButAdd.ShowDropDownArrow = True
        End Sub
        Public Sub ResetButtons()
            tsButAdd.DropDownItems.Clear()
            tsButAdd.ShowDropDownArrow = False
        End Sub

        Private Sub lb_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvSummary.SelectedIndexChanged
            If Not bRefreshing Then
                oSelected = Nothing
                For Each lvi As ListViewItem In lvSummary.SelectedItems
                    oSelected = ItemSelector(lvi)
                Next
                RaiseEvent SelectedChanged(oSelected)
            End If
        End Sub

        Private Sub tsCbShowDel_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsCbShowDel.CheckedChanged
            RefreshData()
        End Sub

        Private Sub tsButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsButAdd.Click
            RaiseEvent AddItem()
        End Sub

        Public Sub New()
            InitializeComponent()
        End Sub
    End Class

End Namespace
it's a generic class for displaying a interactive summary of an object in a list view

i provide specialisation in inheriting classes and in the Overridable ToLVI Function
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.
Reply With Quote
  #6 (permalink)  
Old 04-19-09, 02:48 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Thanks for sharing m8. Glad you got it sorted.
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Reply With Quote
  #7 (permalink)  
Old 04-20-09, 03:06 AM
m.timoney's Avatar
m.timoney m.timoney is offline
Newbie Coder
 
Join Date: Jan 2007
Location: Leicester - UK
Posts: 76
Thanks: 1
Thanked 0 Times in 0 Posts
always a pain debugging stuff with events. but they're too useful not to use
__________________
Definition of a Beginner, Someone who doesn't know the rules.

Definition of an Expert, Someone who knows when to ignore the rules.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
reference not set to an instance of an object. Please help! DonLindon ASP.NET 10 12-26-07 12:37 PM
Object reference not set to an instance of an object. Cabal Windows .NET Programming 2 02-15-07 05:40 AM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
Error in OleDBConnection : Object reference not set to an instance of an object pvsunil Windows .NET Programming 1 04-22-05 01:45 PM
Object Oriented Programming Stefan PHP 29 12-30-03 11:22 AM


All times are GMT -5. The time now is 02:17 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.