Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding NullReferenceException

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
US
Hello all,

I've got a strange problem with a NullReferenceException, and I've been searching for the item that hasn't been set and I just can't seem to find it. (No source code available at location). Oh and the times I get the NullReferenceException appear to be random or I don't get them all the time.

I'm working on a control. My solution has just the test project and the control project in it. The only thing the test project does is show my control, a listbox, and a button and there is not code (save for what the From designer put in there).

I've been commenting out various bits of my control and can't seem to find which subroutine has the offending code. So I put all the code in all the subroutines I don't have commented out in Try...Catch blocks hoping to catch where it was throwing it, but that didn't work either.

Does anyone know how to hunt for a NullReferenceException that is defeating all those debug methods?
 
Are you getting the JIT error? or is a catchable exception being thrown?

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Well my Try...Catch statement is not catching it, so I'm pretty sure it's the JIT error.

I've given all the Subroutines the following format:
Code:
Public Sub Subname()
  Try
    'Sub code here
  Catch Ex As Exception
    MsgBox("Subname")
  End Try
End Sub
The message box does not fire for any of the routines.
 
Exceptions w/o source code can be a b! to track down. The 2 most likely culprits are pre-compiled libraries (where there is no code to break to) and multithreading where a ThreadAbortException is thrown.

So, lets start there, any threading or 3rd party references?

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Here are my imports:

Code:
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel

The class has one thread instance:
Code:
Private tdScroll As Threading.Thread

Although I had all the code using tdScroll commented out, so I don't see how it would be using that.
 
What's the rest of the code look like?

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Well - most of it was commented out, so I started uncommenting it little by little. Here is the first bit I uncommented (drawing the control border).

Code:
Private Sub DrawBorder(ByVal g As Graphics)
        Try
            Select Case _BorderStyle
                Case BorderStyle.None
                    _BorderWidth = 0
                Case BorderStyle.FixedSingle
                    _BorderWidth = 1
                    g.DrawLine(Pens.Black, 0, 0, Me.Width, 0)
                    g.DrawLine(Pens.Black, 0, 0, 0, Me.Height)
                    g.DrawLine(Pens.Black, 0, Me.Height - 1, Me.Width, Me.Height - 1)
                    g.DrawLine(Pens.Black, Me.Width - 1, 0, Me.Width - 1, Me.Height)
                Case BorderStyle.Fixed3D
                    _BorderWidth = 2
                    Dim p1 As New Pen(Color.FromArgb(172, 168, 153))
                    Dim p2 As New Pen(Color.FromArgb(113, 111, 100))
                    Dim p3 As New Pen(Color.White)
                    Dim p4 As New Pen(Color.FromArgb(241, 239, 226))
                    'light gray
                    g.DrawLine(p1, 0, 0, Me.Width - 2, 0)
                    g.DrawLine(p1, 0, 0, 0, Me.Height - 2)
                    'dark gray
                    g.DrawLine(p2, 1, 1, Me.Width - 3, 1)
                    g.DrawLine(p2, 1, 1, 1, Me.Height - 3)
                    'white
                    g.DrawLine(p3, Me.Width - 1, 0, Me.Width - 1, Me.Height)
                    g.DrawLine(p3, 0, Me.Height - 1, Me.Width, Me.Height - 1)
                    'off-white
                    g.DrawLine(p4, Me.Width - 2, 1, Me.Width - 2, Me.Height - 2)
                    g.DrawLine(p4, 1, Me.Height - 2, Me.Width - 3, Me.Height - 2)
            End Select
        Catch ex As Exception
            MsgBox("DrawBorder")
        End Try
    End Sub

Don't know if this is relevant but I'm inheriting ListControl, in an effort to duplicate/enhance the ListBox control without using OwnerDraw.

I didn't notice any problems uncommenting code until I started drawing the items (could be the random error didn't have time to show itself before) which was the next part I uncommented. Here is the code I'm using for that (I'm using a custom collection class to store the items - for Multi-column).

Code:
   Private Sub DrawItems(ByVal g As Graphics)
        Try
            Dim width As Integer
            If _scrollVisible Then
                width = (Me.Width - (_BorderWidth * 2)) - (21 * dpiXRatio)
            Else
                width = (Me.Width - (_BorderWidth * 2))
            End If
            If _items.MyCount = 0 Then
                drawnItems = New Bitmap(width, ItemHeight)
            Else
                drawnItems = New Bitmap(width, ItemHeight * _items.MyCount)
            End If
            Dim item As MCListBoxItem
            Dim x As Integer = 0
            Dim y As Integer = 0
            Dim i As Integer = 0
            For Each item In _items
                Dim p As New Point(x, y)
                Dim h As Single
                If i = _selIndex Then
                    h = OnDrawItem(item, p, g, True)
                Else
                    h = OnDrawItem(item, p, g)
                End If
                y += h
                i += 1
            Next
            If i = 0 Then
                Dim p As New Point(x, y)
                item = New MCListBoxItem
                item.Text = Me.Name
                OnDrawItem(item, p, g)
            End If
            curItemTop = minDispItem * ItemHeight
            toTop = curItemTop
            g.DrawImage(drawnItems, _BorderWidth, _BorderWidth, New Rectangle(0, minDispItem * ItemHeight, drawnItems.Width, Me.Height - (_BorderWidth * 2)), GraphicsUnit.Pixel)
        Catch ex As Exception
            MsgBox("DrawItems")
        End Try
    End Sub
Private Function OnDrawItem(ByVal item As MCListBoxItem, ByVal p As Point, ByVal ig As Graphics, Optional ByVal selected As Boolean = False) As Single
        Try
            Dim g As Graphics = Graphics.FromImage(drawnItems)
            Dim height As Single = g.MeasureString(item.Text, Me.Font).Height
            'g.DrawString(item.Text, Me.Font, New SolidBrush(Me.ForeColor), p.X, p.Y)
            If selected Then
                Dim b As New SolidBrush(Color.FromArgb(49, 106, 197))
                g.FillRectangle(b, p.X, p.Y, Me.Width - 4, ItemHeight)
                g.DrawString(item.Text, Me.Font, New SolidBrush(Color.White), p.X, p.Y)
                Dim pn As New Pen(Color.FromArgb(110, 120, 150))
                If Columns > 1 Then
                    g.DrawLine(pn, ColumnWidths(0), p.Y, ColumnWidths(0), p.Y + ItemHeight)
                End If
            Else
                Dim b As New SolidBrush(Me.BackColor)
                g.FillRectangle(b, p.X, p.Y, Me.Width - 4, ItemHeight)
                g.DrawString(item.Text, Me.Font, New SolidBrush(Me.ForeColor), p.X, p.Y)
                Dim pn As New Pen(Color.FromArgb(192, 192, 192))
                If Columns > 1 Then
                    g.DrawLine(pn, ColumnWidths(0), p.Y, ColumnWidths(0), p.Y + ItemHeight)
                End If
            End If
            Return _ItemHeight
        Catch ex As Exception
            MsgBox("OnDrawItem")
        End Try
    End Function

I don't currently have the other columns showing just yet.
 
Are you writing to a form from a thread other than the one which created the form? That's a big no-no.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Nope - this is a control it is only drawing to itself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top