Results 1 to 4 of 4

Thread: [RESOLVED] Debugging error: "Object reference not set to an instance of an object"

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    22

    Resolved [RESOLVED] Debugging error: "Object reference not set to an instance of an object"

    I am in the process of writing a graphing calculator (just for fun) and I just coded a function that will draw the axes on the graph. Before testing, I could successfully run my program. Now, however, it stops debugging and gives me the error in the title.
    Here's the complete error it gives me in the output module:
    A first chance exception of type 'System.NullReferenceException' occurred in Graphing Calculator.exe
    An unhandled exception of type 'System.InvalidOperationException' occurred in Graphing Calculator.exe
    Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
    The program '[6560] Graphing Calculator.vshost.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff).
    I'm not sure why it's doing this, and have searched for a few hours trying to resolve this (which seems to be decently common) to no avail. I have attatched a .zip folder (Graphing Calculator.zip) of my project to this post if you want to take a look at it. Also, here is my code for drawing the axes:
    Code:
    Private Function drawScale()
            'DECLARE & UPDATE SOME VARIABLES
            Dim n As Integer
            Me.pbGraphGraphics.Refresh()
            Dim G As Graphics
            G = pbGraphGraphics.CreateGraphics
            xSclPxl = pbGraphGraphics.Width / ((xMin - xMax) / xScl)
            ySclPxl = pbGraphGraphics.Height / ((yMin - yMax) / yScl)
            Dim xTcks As Decimal = (xMax - xMin) / xScl
            Dim yTcks As Decimal = (yMax - yMin) / yScl
    
            'DRAWS X-AXIS
            Dim xx1 As Decimal = pbGraphGraphics.Left
            Dim xx2 As Decimal = pbGraphGraphics.Left + pbGraphGraphics.Width
            Dim xy1 As Decimal = pbGraphGraphics.Top + pbGraphGraphics.Height / (yMax / ySclPxl)
            Dim xy2 As Decimal = xy1
            G.DrawLine(Pens.Black, xx1, xy1, xx2, xy2)
            'DRAWS Y-AXIS
            Dim yx1 As Decimal = pbGraphGraphics.Left + pbGraphGraphics.Width / (xMax / xSclPxl)
            Dim yx2 As Decimal = yx1
            Dim yy1 As Decimal = pbGraphGraphics.Top
            Dim yy2 As Decimal = pbGraphGraphics.Top + pbGraphGraphics.Height
            G.DrawLine(Pens.Black, yx1, yx2, yy1, yy2)
    
            'DRAWS X-AXIS SCALE TICKS
            n = 1
            Dim xtx1 As Decimal = pbGraphGraphics.Left
            Dim xtx2 As Decimal = pbGraphGraphics.Left + (xSclPxl * n)
            Dim xty1 As Decimal = xy1 - 2
            Dim xty2 As Decimal = xy1 + 2
            Dim xt As Integer = 0
            For xt = 0 To ((xMax - xMin) / xScl)
                G.DrawLine(Pens.Black, xtx1, xty1, xtx2, xty2)
                n = n + 1
                xtx2 = pbGraphGraphics.Left + (xSclPxl * n)
            Next
    
            'DRAWS Y-AXIS SCALE TICKS
            n = 1
            Dim ytx1 As Decimal = yx1 - 2
            Dim ytx2 As Decimal = yx1 + 2
            Dim yty1 As Decimal = pbGraphGraphics.Top
            Dim yty2 As Decimal = pbGraphGraphics.Top + (ySclPxl * n)
            Dim yt As Integer = 0
            For yt = 0 To ((yMax - yMin) / yScl)
                G.DrawLine(Pens.Black, ytx1, yty1, ytx2, yty2)
                n = n + 1
                ytx2 = pbGraphGraphics.Left + (ySclPxl * n)
            Next
    
        End Function
    Even after commenting this out and getting rid of the places where it's called, my program still doesn't function and gives me the same error. Any help is greatly appreciated! Thanks!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Debugging error: "Object reference not set to an instance of an object"

    Whenever you get that error, examine the line where the exception was thrown. One of the objects is Nothing, and finding that object is the first step. It is often the only one you really need to take, too, but it is essential.

    So, which line is the exception happening on?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2013
    Posts
    22

    Re: Debugging error: "Object reference not set to an instance of an object"

    Alright, with a little research, I found that I set a variable to an object not yet initialized, on lines 39 and 40.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Debugging error: "Object reference not set to an instance of an object

    That will do it.

    There are some rare cases where the IDE doesn't take you to the actual line that caused the exception, but in most cases it will. As long as the IDE takes you to the line that thew the exception, this is a straightforward exception to diagnose....which is good, because it may be the single most common exception to get. The most common time that the IDE does NOT take you to the line that thew the exception is when the exception was thrown during class initialization. Those can get tricky.
    My usual boring signature: Nothing

Tags for this Thread

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