locked
how to solve Object reference not set to an instance of an object error in vb.net?

    Question

  • i have the code like this.

     

     

     

    1.   Private Sub ButtonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
    2.         Dim StrSql As New System.Text.StringBuilder
    3.         Dim counter As Integer
    4.         Dim ObjDB As clsDB = clsDB.GetDBInstatce
    5.         Try
    6.             If gridItemMasterMaintainance.Rows.Count = Nothing Then Exit Sub
    7.             If clsMsgAlert.DisplayMsgBox(clsGeneral.LConvert(261), MsgBoxStyle.YesNo) = False Then Exit Sub
    8.             ObjDB.BeginTransaction()
    9.             For counter = 1 To gridItemMasterMaintainance.Rows.Count - 1
    10.               
    11.                 StrSql.Append("Update MOPS_MST01_ITEM set ")
    12.                 StrSql.Append("T01EXPCATEGORY='" & gridItemMasterMaintainance.Rows(counter).Item(2).ToString & "' ")
    13.                 StrSql.Append("where T01ITEMNO='" & gridItemMasterMaintainance.Rows(counter).Item(0).ToString & "'")
    14.                 ObjDB.ExecuteSql(StrSql.ToString) : StrSql.Length = 0
    15.             Next
    16.             ObjDB.CommitTransaction()
    17.             If clsMsgAlert.DisplayMsgBox(clsGeneral.LConvert(126), vbYesNo) = True Then
    18.                 Call btnLoad_Click(sender, e)
    19.             End If
    20.         Catch ex As InvalidCastException
    21.             ObjDB.RollBack()
    22.             clsMsgAlert.SetErrorMessage = clsGeneral.LConvert(1323) & " " & Me.Name & " " & clsGeneral.LConvert(1324) & " " & clsMsgAlert.GetCurrentMethod(New StackFrame)
    23.             clsMsgAlert.DisplayErrorMessage(clsMsgAlert.GetErrorMessage, ex.Message, MsgBoxStyle.Exclamation)
    24.         Catch ex As NullReferenceException
    25.             ObjDB.RollBack()
    26.             clsMsgAlert.SetErrorMessage = clsGeneral.LConvert(1323) & " " & Me.Name & " " & clsGeneral.LConvert(1324) & " " & clsMsgAlert.GetCurrentMethod(New StackFrame)
    27.             clsMsgAlert.DisplayErrorMessage(clsMsgAlert.GetErrorMessage, ex.Message, MsgBoxStyle.Exclamation)






    In the 13th line, i am getting the error like 

    Object reference not set to an instance of an object. i have used the gridview . when i click on save button, i am getting the error like this.save button is working when data is there in gridview. but when i click on save button, i am getting error if there is no data in gridview. please help me anybody... 


    SampathP
    Tuesday, January 18, 2011 11:00 AM

Answers

  • Hi,

    change the line 10 to:

    For counter = 0 To gridItemMasterMaintainance.Rows.Count - 1 
    
    


    Arrays(here Rows property) in .NET are starting at zero

     


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Tuesday, January 18, 2011 11:22 AM