Object reference not set to an instance of an object.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Abhiraj Chauhan
    New Member
    • Oct 2008
    • 19

    Object reference not set to an instance of an object.

    I am trying to draw line.....Actual ly these codes were previously in vb 6...i tried to change them in vb.net....but every time i try to run the application i got this error ."Object reference not set to an instance of an object"...plz help me regerding this error....here are my code

    Code:
    Sub BeamDraw(ByRef DrawControl As PaintEventArgs, ByRef ds As DAO.Recordset)
            Dim Y5 As Double
            Dim X5 As Double
            Dim Y2 As Double
            Dim X2 As Double
            Dim Y3 As Double
            Dim X3 As Double
            Dim Y1 As Double
            Dim X1 As Double
            Dim szcolumn As String
            Dim X As Single
            Dim Y As Single
            Dim Dc As DAO.Recordset
            Dim DCC As DAO.Recordset
            DCC = CurDb.OpenRecordset("Columns", DAO.RecordsetTypeEnum.dbOpenDynaset)
            Dc = CurDb.OpenRecordset("Columns", DAO.RecordsetTypeEnum.dbOpenDynaset)
            While Not ds.EOF
                szcolumn = "Cname='" + ds.Fields(0).Value + "'"
                DCC.FindFirst(szcolumn)
                X = DCC.Fields(4).Value
                Y = DCC.Fields(5).Value
                X1 = DCC.Fields(4).Value
                Y1 = DCC.Fields(5).Value
                FlushDistances(DCC, X, Y)
                X3 = X
                Y3 = Y
                If StrComp(Left(DCC.Fields(0).Value, 1), "C") = 0 Then
                    Select Case DCC.Fields(1).Value
                        Case "H"
                            DrawControl.Graphics.DrawLine(Pens.OrangeRed, CInt(X - DCC.Fields(2).Value / 2), CInt(Y - DCC.Fields(3).Value / 2) - CInt(X + DCC.Fields(2).Value / 2), CInt(Y + DCC.Fields(3).Value / 2), B)
                            
                     
                    End Select
                Else
                    Select Case DCC.Fields(1).Value
                        Case "H"              
                            DrawControl.Graphics.DrawLine(Pens.OrangeRed, CInt(X - DCC.Fields(2).Value / 2), CInt(Y - DCC.Fields(3).Value / 2) - CInt(X + DCC.Fields(2).Value / 2), CInt(Y + DCC.Fields(3).Value / 2), B)
                      
                    End Select
                End If
                If Enlarge Or BeamSelect Then
                    Column_Dis(DCC.Fields(4).Value + DCC.Fields(2).Value / 2, DCC.Fields(5).Value - DCC.Fields(3).Value / 2, DCC.Fields(0).Value, DrawControl)
                End If
                szcolumn = "Cname='" + ds.Fields(1).Value + "'"
                Dc.FindFirst(szcolumn)
                X = (Dc.Fields(4).Value)
                Y = (Dc.Fields(5).Value)
                X2 = Dc.Fields(4).Value
                Y2 = Dc.Fields(5).Value
                FlushDistances(Dc, X, Y)
                X5 = X
                Y5 = Y
                If X1 = X2 Then
                    CenterFlag = "BeamVertical"
                Else
                    If Y1 = Y2 Then
                        CenterFlag = "BeamHorizontal"
                    End If
                End If
                If StrComp(Left(Dc.Fields(0).Value, 1), "C") = 0 Then
                    Select Case Dc.Fields(1).Value
                        Case "H"
                            DrawControl.Graphics.DrawLine(Pens.OrangeRed, CInt(X - Dc.Fields(2).Value / 2), CInt(Y - Dc.Fields(3).Value / 2), CInt(X + Dc.Fields(2).Value / 2), CInt(Y + Dc.Fields(3).Value / 2))
                                                      
                    End Select
                Else
                    Select Case Dc.Fields(1).Value
                        Case "H"
                           
                            DrawControl.Graphics.DrawLine(Pens.OrangeRed, CInt(X - Dc.Fields(2).Value / 2), CInt(Y - Dc.Fields(3).Value / 2) - CInt(X + Dc.Fields(2).Value / 2), CInt(Y + Dc.Fields(3).Value / 2), B)
                          
                    End Select
                End If
    
                If Enlarge Or BeamSelect Then
                    Column_Dis(Dc.Fields(4).Value + Dc.Fields(2).Value / 2, Dc.Fields(5).Value - Dc.Fields(3).Value / 2, Dc.Fields(0).Value, DrawControl)
                End If
                ShowBeam(ds, DCC, DCC, DrawControl, X3, Y3, X5, Y5, CenterFlag)
                ds.MoveNext()
            End While
        End Sub
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    It would help if you could provide more information. Do you have a line number where it is occurring or can you trap it?

    Comment

    • Abhiraj Chauhan
      New Member
      • Oct 2008
      • 19

      #3
      Well thanks for replying....... .Actually in my project i have 2 forms one consist of a datagrid and other form consisting of a picture box,,,,,and now when you enter data in first form you can see the drawing of your data in the other form.

      also i am using vb.net for first time so if u can help me what should i use for drawing lines ...... system.drawing. graphics or system.windows. forms.paintEven targs

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Originally posted by Abhiraj Chauhan
        I am trying to draw line.....Actual ly these codes were previously in vb 6...i tried to change them in vb.net....but every time i try to run the application i got this error ."Object reference not set to an instance of an object"...plz help me regerding this error....here are my code

        Code:
        Sub BeamDraw(ByRef DrawControl As PaintEventArgs, ByRef ds As DAO.Recordset)
                Dim Y5 As Double
                Dim X5 As Double
                Dim Y2 As Double
                Dim X2 As Double
                Dim Y3 As Double
                Dim X3 As Double
                Dim Y1 As Double
                Dim X1 As Double
                Dim szcolumn As String
                Dim X As Single
                Dim Y As Single
                Dim Dc As DAO.Recordset
                Dim DCC As DAO.Recordset
                DCC = CurDb.OpenRecordset("Columns", DAO.RecordsetTypeEnum.dbOpenDynaset)
                Dc = CurDb.OpenRecordset("Columns", DAO.RecordsetTypeEnum.dbOpenDynaset)
                While Not ds.EOF
                    szcolumn = "Cname='" + ds.Fields(0).Value + "'"
                    DCC.FindFirst(szcolumn)
                    X = DCC.Fields(4).Value
                    Y = DCC.Fields(5).Value
                    X1 = DCC.Fields(4).Value
                    Y1 = DCC.Fields(5).Value
                    FlushDistances(DCC, X, Y)
                    X3 = X
                    Y3 = Y
                    If StrComp(Left(DCC.Fields(0).Value, 1), "C") = 0 Then
                        Select Case DCC.Fields(1).Value
                            Case "H"
                                DrawControl.Graphics.DrawLine(Pens.OrangeRed, CInt(X - DCC.Fields(2).Value / 2), CInt(Y - DCC.Fields(3).Value / 2) - CInt(X + DCC.Fields(2).Value / 2), CInt(Y + DCC.Fields(3).Value / 2), B)
                                
                         
                        End Select
                    Else
                        Select Case DCC.Fields(1).Value
                            Case "H"              
                                DrawControl.Graphics.DrawLine(Pens.OrangeRed, CInt(X - DCC.Fields(2).Value / 2), CInt(Y - DCC.Fields(3).Value / 2) - CInt(X + DCC.Fields(2).Value / 2), CInt(Y + DCC.Fields(3).Value / 2), B)
                          
                        End Select
                    End If
                    If Enlarge Or BeamSelect Then
                        Column_Dis(DCC.Fields(4).Value + DCC.Fields(2).Value / 2, DCC.Fields(5).Value - DCC.Fields(3).Value / 2, DCC.Fields(0).Value, DrawControl)
                    End If
                    szcolumn = "Cname='" + ds.Fields(1).Value + "'"
                    Dc.FindFirst(szcolumn)
                    X = (Dc.Fields(4).Value)
                    Y = (Dc.Fields(5).Value)
                    X2 = Dc.Fields(4).Value
                    Y2 = Dc.Fields(5).Value
                    FlushDistances(Dc, X, Y)
                    X5 = X
                    Y5 = Y
                    If X1 = X2 Then
                        CenterFlag = "BeamVertical"
                    Else
                        If Y1 = Y2 Then
                            CenterFlag = "BeamHorizontal"
                        End If
                    End If
                    If StrComp(Left(Dc.Fields(0).Value, 1), "C") = 0 Then
                        Select Case Dc.Fields(1).Value
                            Case "H"
                                DrawControl.Graphics.DrawLine(Pens.OrangeRed, CInt(X - Dc.Fields(2).Value / 2), CInt(Y - Dc.Fields(3).Value / 2), CInt(X + Dc.Fields(2).Value / 2), CInt(Y + Dc.Fields(3).Value / 2))
                                                          
                        End Select
                    Else
                        Select Case Dc.Fields(1).Value
                            Case "H"
                               
                                DrawControl.Graphics.DrawLine(Pens.OrangeRed, CInt(X - Dc.Fields(2).Value / 2), CInt(Y - Dc.Fields(3).Value / 2) - CInt(X + Dc.Fields(2).Value / 2), CInt(Y + Dc.Fields(3).Value / 2), B)
                              
                        End Select
                    End If
        
                    If Enlarge Or BeamSelect Then
                        Column_Dis(Dc.Fields(4).Value + Dc.Fields(2).Value / 2, Dc.Fields(5).Value - Dc.Fields(3).Value / 2, Dc.Fields(0).Value, DrawControl)
                    End If
                    ShowBeam(ds, DCC, DCC, DrawControl, X3, Y3, X5, Y5, CenterFlag)
                    ds.MoveNext()
                End While
            End Sub
        Total stab in the dark by someone who doesn't do VB...

        In line 78 you make use of CenterFlag variable.
        I see no place where you create this variable (Looks like that is done with the DIM statement as in line 10 ??)

        Assuming this is a global variable created elsewhere... Is it ever initialized to a default value? CenterFlag only gets set in Verticle in line 54 if X1=X2 or to Horizontal in line 57 if Y1=Y2. What happens if neither of these conditions exist? Does CenterFlag remain unset later when it is used in line 78?

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Abhiraj Chauhan
          Well thanks for replying....... .Actually in my project i have 2 forms one consist of a datagrid and other form consisting of a picture box,,,,,and now when you enter data in first form you can see the drawing of your data in the other form.

          also i am using vb.net for first time so if u can help me what should i use for drawing lines ...... system.drawing. graphics or system.windows. forms.paintEven targs
          Why don't you just tell us the number reported for that exception?

          Comment

          • Abhiraj Chauhan
            New Member
            • Oct 2008
            • 19

            #6
            Originally posted by tlhintoq
            Total stab in the dark by someone who doesn't do VB...

            In line 78 you make use of CenterFlag variable.
            I see no place where you create this variable (Looks like that is done with the DIM statement as in line 10 ??)

            Assuming this is a global variable created elsewhere... Is it ever initialized to a default value? CenterFlag only gets set in Verticle in line 54 if X1=X2 or to Horizontal in line 57 if Y1=Y2. What happens if neither of these conditions exist? Does CenterFlag remain unset later when it is used in line 78?
            Then it Will give us a diagonal Connection....W ell Thank u .....

            Comment

            Working...